Skip to main content

Sort a multidimensional array in PHP?

the most challenge question in the past was soring a multidimensional arrays in PHP, now lets make it in an easy way

You have your array like this 

 

$array = [];
$array[] = array('name' => 'name1', 'age' => 85);
$array[] = array('name' => 'name2', 'age' => 15);
$array[] = array('name' => 'name3', 'age' => 10);
$array[] = array('name' => 'name4', 'age' => 59);

the easy way is to use these command 

 

$columns = array_column($array, 'name');
array_multisort($columns, SORT_ASC, $array);   // SORT_DESC for descending 

 

 

 

Tags