Home >Backend Development >PHP Tutorial >How to Sort a Multidimensional PHP Array by an Inner Field\'s Value?
Sorting a Multidimensional Array by Inner Field in PHP
Suppose we have a multidimensional array resembling a database table, with each outer array element representing a row and each inner array containing field names and values. We want to sort the rows (outer array elements) by a specific field, such as "price."
To achieve this, we can utilize the following PHP function:
array_multisort(array_column($yourArray, "price"), SORT_ASC, $yourArray);
This line accomplishes our desired result in a concise manner. The array_multisort() function takes three arguments:
By chaining these functions together, we can effectively sort the outer array elements by the "price" field. It's important to note that this process overwrites the original $yourArray variable, so assigning the result to a different variable is advisable if the original order needs to be preserved.
Updates:
$col = array_column($yourArray, "price"); array_multisort($col, SORT_ASC, $yourArray);
The above is the detailed content of How to Sort a Multidimensional PHP Array by an Inner Field\'s Value?. For more information, please follow other related articles on the PHP Chinese website!