
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
How do I sort a multidimensional array by one of the fields of the inner array in PHP?
The usort function can be used to sort a multidimensional array. It sorts with the help of a user defined function.
Below is a sample code demonstration −
Example
function compare_array($var_1, $var_2) { if ($var_1["price"] == $var_2["price"]) { return 0; } return ($var_1["price"] < $var_2["price"]) ? -1 : 1; } usort($my_Array,"compare_array") $var_1 = 2 $var_2 = 0
Output
This will produce the following output −
1
Explanation − We have declared var_1 and var)2 with integer values. They are compared and the result is returned.
- Related Articles
- Sort php multidimensional array by sub-value in PHP
- Sort multidimensional array by multiple keys in PHP
- PHP Multidimensional Array.
- MongoDB query to sort by the sum of specified object inside inner array?
- How to sort inner array in MongoDB?
- How to convert Multidimensional PHP array to JavaScript array?
- How do I sort a two-dimensional array in C#
- How to check for multidimensional nature of an array in PHP
- Sort an array of dates in PHP
- How do I work with array fields in MongoDB to match all?
- Sort array based on presence of fields in objects JavaScript
- How to do multidimensional array intersection using JavaScript?
- How do I sort a list of dictionaries by values of the dictionary in Python?
- How do I search according to fields in inner classes using MongoDB db.coll.find()?
- Initialization of a multidimensional array in C

Advertisements