在PHP中按值对3级多维数组进行排序

时间:2021-02-01 22:04:40

Here is the array,

这是数组,

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
        )
)

most of the usort examples are just 2 level dimension array. I couldn't find any example for 3 level.

大多数usort的例子都是二维数组。我找不到三个层次的例子。

In this case i wanted to sort the smallest points to show first. Item-A will be the first and Item-B will be the 2nd.

在这种情况下,我想先对最小的点进行排序。itema将是第一个,itemb将是第二个。

2 个解决方案

#1


0  

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);

this is working...

这是工作…

#2


0  

Try this:

试试这个:

  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);

#1


0  

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);

this is working...

这是工作…

#2


0  

Try this:

试试这个:

  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);