Answer the question
In order to leave comments, you need to log in
Sorting a multidimensional array?
Let's say we have a multidimensional array:
$data=array(
array('text'=>'str1', 'year'=>'2010', 'author'=>10, 'theme' => 4),
array('text'=>'str2', 'year'=>'2011', 'author'=>10, 'theme' => 0),
array('text'=>'str3', 'year'=>'2009', 'author'=>20, 'theme' => 3),
array('text'=>'str4', 'year'=>'2010', 'author'=>30, 'theme' => 1),
array('text'=>'str5', 'year'=>'2010', 'author'=>20, 'theme' => 0),
array('text'=>'str6', 'year'=>'2011', 'author'=>10, 'theme' => 2),
array('text'=>'str7', 'year'=>'2011', 'author'=>20, 'theme' => 4),
array('text'=>'str8', 'year'=>'2009', 'author'=>20, 'theme' => 0),
);
Answer the question
In order to leave comments, you need to log in
uasort($data, function($a, $b) {
return $a['theme']==0 || $b['theme']==0 ? -1 : 0;
});
Offhand.
Go through the array, in the ife check theme for 0, if I add to one array (or immediately move it to the beginning) everything else to another (if it is divided into two arrays)
Then merge them (again, if it is divided into two arrays )
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question