
- 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
Sort php multidimensional array by sub-value in PHP
The ‘usort’ function can be used to sort multidimensional arrays in PHP. It sorts an array based on a user-defined criteria −
Example
<?php function my_sort($a,$b) { if ($a==$b) return 0; return ($a<$b)?-1:1; } $a=array(4,2,81,63); usort($a,"my_sort"); $arrlength=count($a); for($x=0;$x<$arrlength;$x++) { echo $a[$x]; echo "<br>"; } ?>
Output
This will produce the following output −
2 4 63 81
An array with 4 elements is declared and this array is passed to the usort function, as well as calling the user-defined ‘my_sort’ function on the elements to make sure that the sorting takes place is ascending order.
- Related Articles
- Sort multidimensional array by multiple keys in PHP
- PHP Multidimensional Array.
- How do I sort a multidimensional array by one of the fields of the inner array in PHP?
- Multidimensional arrays in PHP
- How to convert Multidimensional PHP array to JavaScript array?
- Sort an array of dates in PHP
- How to check for multidimensional nature of an array in PHP
- PHP Declaring sub-namespaces
- sort() function in PHP
- Sort MongoDB Collection by Array value?
- PHP program to sort dates given in the form of an array
- Search for partial value match in an Array in PHP
- What is Pass By Reference and Pass By Value in PHP?
- array() function in PHP
- PHP Array Operators

Advertisements