51

重複の可能性:
内部キーで多次元配列を
並べ替える方法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の順序で表示されますか?

4

1 に答える 1

130

usortこの目的のために明示的に構築されたものを使用してください。

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

usort($array, "cmp");
于 2012-04-03T19:28:39.140 に答える