PHP二维数组按元素的某个键名排序

$dbattr = Array(
    1 => Array
        (
            "goods_attr_id" => 359,
            "goods_id" => 60,
            "attr_id" => 3
        ),
    2 => Array
        (
            "goods_attr_id" => 360,
            "goods_id" => 60,
            "attr_id" => 4
        ),
    3 => Array
        (
            "goods_attr_id" => 366,
            "goods_id" => 60,
            "attr_id" => 2
        ),
    4 => Array
        (
            "goods_attr_id" => 363,
            "goods_id" => 60,
            "attr_id" => 8
        )

);
array_multisort(array_column($dbattr,'attr_id'),SORT_DESC,$dbattr);
print_r($attr_id);

注释:

array_column(数组,数组中的某个键名)  返回数组中某个单一列的值(返回的二维数组仅含attr_id元素),注意:该函数运行环境须大于php5.5

array_multisort(一维数组,排序方式(SOTR_ASC或SOTR_DESC),二维数组)

你可能感兴趣的:(PHP)