传递给array_sort()的参数2必须是可调用的,字符串给定

时间:2015-11-07 12:01:12

标签: php arrays sorting laravel

我有以下数组:

  array (size=3)
    0 => 
      array (size=3)
        'id' => int 18
        'class' => string 'VIP' (length=3)
        'fee' => float 20
    1 => 
      array (size=3)
        'id' => int 19
        'class' => string 'VVIP' (length=4)
        'fee' => float 50
    2 => 
      array (size=3)
        'id' => int 20
        'class' => string 'STANDARD' (length=8)
        'fee' => float 5

  array (size=3)
    0 => 
      array (size=3)
        'id' => int 19
        'class' => string 'VVIP' (length=4)
        'fee' => int 50
    1 => 
      array (size=3)
        'id' => int 18
        'class' => string 'VIP' (length=3)
        'fee' => int 20
    2 => 
      array (size=3)
        'id' => int 20
        'class' => string 'STANDARD' (length=8)
        'fee' => int 5

现在,我正尝试使用array_sort升序中使用id对它们进行排序。:

$array_1 = array_sort($array_1, 'id', SORT_ASC);
$array_2 = array_sort($array_2, 'id', SORT_ASC);

但是我一直收到以下错误:

  

传递给array_sort()的参数2必须是可调用的,字符串给定

1 个答案:

答案 0 :(得分:1)

array_sort的第二个参数应该是闭包,而不是字符串:

$array_1 = array_sort( $array_1, function($value){
    return $value['id'];
});