0

我有一个看起来像这样的数组:

array(
  (int) 0 => array(
    'SomeItem' => array(
        'id' => '1',
        'number' => '3',
        'created' => '2012-07-20 23:34:00',
        'modified' => '2012-07-20 23:34:00'
      )
  )
  (int) 1 => array(
    'SomeItem' => array(
        'id' => '1',
        'number' => '6',
        'created' => '2012-07-20 23:34:00',
        'modified' => '2012-07-20 23:34:00'
    )
  )
)

我将如何按“数字”对其进行排序,以便我可以将其按数字升序排列,同时仍保持结构?

谢谢!

4

1 回答 1

1

使用闭包:

usort($array, function($a, $b)
        {
            if ($a['SomeItem']['number'] == $b['SomeItem']['number'])
                return 0;

            return ($a['SomeItem']['number'] < $b['SomeItem']['number']) ? -1 : 1;
        }
    );  

没有

function cmp($a, $b)
{
    if ($a['SomeItem']['number'] == $b['SomeItem']['number'])
        return 0;

    return ($a['SomeItem']['number'] < $b['SomeItem']['number']) ? -1 : 1;
}
usort($array, 'cmp');
于 2012-07-25T02:55:25.207 回答