How to sort multi-dimensional array (PHP)?

后端 未结 2 364
野趣味
野趣味 2021-01-16 11:25

How to sort this array by pos attribute even though keys (name, store_id, product etc.)

[Attributes] => Array
(         


        
2条回答
  •  长发绾君心
    2021-01-16 12:24

    You could use uasort() which lets you define your sorting logic and also maintains your associative indexes. Please note that it changes your original array and only returns a boolean based on success.

    uasort($your_array, function($a, $b) {
        return $a['pos'] > $b['pos'];
    });
    

    My example works >= PHP 5.3 , but for older versions you can use a normal compare function as well.

    See uasort() Documentation for details.

提交回复
热议问题