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

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

Leave a Comment