A
A
AlexFXIII2016-02-21 23:03:37
PHP
AlexFXIII, 2016-02-21 23:03:37

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),
);

It is necessary to form the data output (say, to a table) in such a way that the lines with 'theme' => 0 would be in the first places, and all other lines would have exactly the same order as in the array.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2016-02-21
@thewind

uasort($data, function($a, $b) {
 return $a['theme']==0 || $b['theme']==0 ? -1 : 0;
});

Try

I
Ivan Kondratiev, 2016-02-21
@inik23

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 question

Ask a Question

731 491 924 answers to any question