在PHP中按值排序一个3级多维数组



这是数组,

array(
[0] => Array
        (
            [IdRedeemProduct] => Item-A
            [RedeemOptions] => Array
                (
                    [0] => Array
                        (
                            [Points] => 1000
                        )
                    [1] => Array
                        (
                            [Points] => 2000
                        )
                    [2] => Array
                        (
                            [Points] => 43000
                        )
                )
            [ProductType] => 1
        )
[1] => Array
        (
            [IdRedeemProduct] => Item-B
            [RedeemOptions] => Array
                (
                    [0] => Array
                        (
                            [Points] => 6200
                        )
                    [1] => Array
                        (
                            [Points] => 53000
                        )
                )
            [ProductType] => 1
        )
)

大多数排序示例只是2级维数组。我找不到任何3级的例子。

在本例中,我想对首先显示的最小点进行排序。第一个是Item-A,第二个是Item-B。

foreach ($filteredResults as $key => $row)
                    {
                        foreach ($row['RedeemOptions'] as $key2 => $option) {
                            $vc_array_name[$key] = $option['Points'];
                        }
                    }
array_multisort($vc_array_name, SORT_ASC, $filteredResults);

试试这个:

  function sort_2d_desc($array, $key) {
    usort($array, function($a, $b) use ($key) {
        return strnatcasecmp($b[$key], $a[$key]);
    });
    return $array;
  }
       $a = [];
    foreach($arr as $key => $val){
        $a[$key] = $this->sort_2d_desc($val['RedeemOptions'], 'Points');
    }
    $newArr = [];
    foreach($arr as $key => $val){
        $newArr[] = ['IdRedeemProduct' => $val['IdRedeemProduct'], 'RedeemOptions' => $a, 'ProductType' => $val['ProductType']];
    }

    print_r($newArr);

相关内容

  • 没有找到相关文章

最新更新