PHP Sort array by field? [duplicate]
Possible Duplicate:
how to sort a multidemensional array by an inner key
How to sort an array of arrays in php?
How can I sort an array like: $array[$i]['title'];
Array structure might be like:
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'
);
So data is displayed in ASC order based off the title field in the array?
Solution 1:
Use usort
which is built explicitly for this purpose.
function cmp($a, $b)
{
return strcmp($a["title"], $b["title"]);
}
usort($array, "cmp");