- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Advertisements