0

我有一个数组,需要排序(基于 id):

Array
(
   [0] => Array
    (
        [qty] => 1
        [id] => 3
        [name] => Name1
        [sku] => Model 1
        [options] => 
        [price] => 100.00
    )
   [1] => Array
    (
        [qty] => 2
        [id] => 1
        [name] => Name2
        [sku] => Model 1
        [options] => Color: <em>Black (+10$)</em>. Memory: <em>32GB (+99$)</em>. 
        [price] => 209.00
    )

)

是否可以对我的数组进行排序以获得输出(基于 id)?

 Array
    (
    [0] => Array
      (
        [qty] => 2
        [id] => 1
        [name] => Name2
        [sku] => Model 1
        [options] => Color: <em>Black (+10$)</em>. Memory: <em>32GB (+99$)</em>. 
        [price] => 209.00
      ) 
    [1] => Array
      (
        [qty] => 1
        [id] => 3
        [name] => Name1
        [sku] => Model 1
        [options] => 
        [price] => 100.00
      )
 )

谢谢!

4

2 回答 2

1

试试喜欢

$id_arr = array();
foreach ($my_arr as $key => $value)
{
    $id_arr[$key] = $value['id'];
}
array_multisort($id_arr, SORT_DESC, $my_arr);

您还可SORT_ASC以下订单。最好添加ORDER BY id到查询中,通过该查询您可以获取此结果数组

于 2013-06-27T10:19:15.813 回答
1
function cmp($a, $b) {
        return $a["id"] - $b["id"];
}
usort($arr, "cmp");//$arr is the array to sort
于 2013-06-27T10:25:16.557 回答