首页 > 解决方案 > 按数组的最高键值获取排序数据

问题描述

我有一个变量 $_SESSION["cart_item"] 它按数组存储购物车项目,如下所示 -

Array
( 
    [0] => Array
        (
            [company] => ABC company 
            [good] => Laptop 
            [price] => 100 
            [ship] => 20 
        )

    [1] => Array
        (
            [company] => ABC company 
            [good] => PC 
            [price] => 20
            [ship] => 70 
        )

    [2] => Array
        (
            [company] => DELL PLC 
            [good] => Hard Drive 
            [price] => 500 
            [ship] => 50 
        )

    [3] => Array
        (
            [company] => ABC company 
            [good] => Bag 
            [price] => 30
            [ship] => 40 
        )

)

我想按公司获取排序后的数据,并且[ship]价值最高。

标签: php

解决方案


$sortdata = sort2d_bycolumn($data,'ship',SORT_DESC);
function sort2d_bycolumn($array, $column, $method)
{
    foreach ($array as $key => $row) {
        $narray[$key]  = $row[$column]; 
    }
    array_multisort($narray, $method, $array);
    return $array;
}

sort2d_bycolumn是排序函数 $array 是您要排序的数组 $column 是需要对数组进行排序的列 $method 值将是SORT_ASC或者SORT_DESC


推荐阅读