I want to sort the array below by 'name'. I have tried several things but I can't figure it out. Any suggestions?
$data = array();
$data[] = array('name'=>'Bill','phone'=>'555-5555');
$data[] = array('name'=>'Joe','phone'=>'555-5554');
...
I want to sort the array below by 'name'. I have tried several things but I can't figure it out. Any suggestions?
$data = array();
$data[] = array('name'=>'Bill','phone'=>'555-5555');
$data[] = array('name'=>'Joe','phone'=>'555-5554');
...
You can use usort()
to sort an array using a custom criteria.
For instance:
function my_sort_by_name($a, $b) {
return strcmp($a['name'], $b['name']);
}
$data = array();
$data[] = array('name'=>'Bill','phone'=>'555-5555');
$data[] = array('name'=>'Joe','phone'=>'555-5554');
usort($data, 'my_sort_by_name');