PHP按字段排序数组?

时间:2012-04-03 19:25:46

标签: php arrays sorting

  

可能重复:
  how to sort a multidemensional array by an inner key
  How to sort an array of arrays in php?

如何对数组进行排序,如:$array[$i]['title'];

数组结构可能如下:

array[0](
'id' => 321,
'title' => 'Some Title',
'slug' => 'some-title',
'author' => 'Some Author',
'author_slug' => 'some-author');

array[1](
'id' => 123,
'title' => 'Another Title',
'slug' => 'another-title',
'author' => 'Another Author',
'author_slug' => 'another-author');

那么数据会根据数组中的标题字段 ASC顺序显示吗?

1 个答案:

答案 0 :(得分:89)

使用为此目的明确构建的usort

function cmp($a, $b)
{
    return strcmp($a["title"], $b["title"]);
}

usort($array, "cmp");