Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I sort a multidimensional array by one of the fields of the inner array in PHP? [duplicate]

Suppose I have an array that mimics a database table. Each array element represents a row, and within each row is another array that contains the field names and values.

Array (     [0] => Array         (             [name] => 'Sony TV'             [price] => 600.00         )      [1] => Array         (             [name] => 'LG TV'             [price] => 350.00         )      [2] => Array         (             [name] => 'Samsung TV'             [price] => 425.00         )   } 

What I want to do is sort the rows (outer array elements) by price. Below is an example of what I want to achieve:

Array (     [0] => Array         (             [name] => 'LG TV'             [price] => 350.00         )      [1] => Array         (             [name] => 'Samsung TV'             [price] => 425.00         )      [2] => Array         (             [name] => 'Sony TV'             [price] => 600.00         )         } 

As you can see, I don't need to preserve the keys of the outer array.

like image 775
Camsoft Avatar asked Mar 11 '10 16:03

Camsoft


People also ask

How do I sort a multidimensional array by column in PHP?

In this example, we will order by volume descending, edition ascending. We have an array of rows, but array_multisort() requires an array of columns, so we use the below code to obtain the columns, then perform the sorting. array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);

How do I sort a multi dimensional array in PHP?

Sorting a multidimensional array by element containing date. Use the usort() function to sort the array. The usort() function is PHP builtin function that sorts a given array using user-defined comparison function. This function assigns new integral keys starting from zero to array elements.

Which function sort multiple array at once?

The array_multisort() function returns a sorted array. You can assign one or more arrays. The function sorts the first array, and the other arrays follow, then, if two or more values are the same, it sorts the next array, and so on.


2 Answers

It is just a one liner

array_multisort( array_column($yourArray, "price"), SORT_ASC, $yourArray ); 

You can find it here as well: http://php.net/manual/en/function.array-multisort.php

search for "array_column" on that manual page.

UPDATE of Dec 1, 2020:

As @Tyler V. mentioned in his comment this syntax may cause errors in newer PHP 7 versions around "only variables can be passed by reference." To avoid those you need to change the one liner to a two liner unfortunately:

$col = array_column( $yourArray, "price" ); array_multisort( $col, SORT_ASC, $yourArray ); 
like image 129
rf1234 Avatar answered Sep 24 '22 02:09

rf1234


You need to use usort, a function that sorts arrays via a user defined function. Something like:

function cmp($a, $b) {     if ($a["price"] == $b["price"]) {         return 0;     }     return ($a["price"] < $b["price"]) ? -1 : 1; }  usort($yourArray,"cmp") 
like image 22
Dancrumb Avatar answered Sep 21 '22 02:09

Dancrumb