PHP multidimensional array sorted based on the value of one of the fields
Oct 09, 2016 am 11:51 AMUsually simple one-dimensional arrays or simple array sorting will not be introduced here. This is mainly for the situations that may be encountered in daily projects, sorting according to one of the multi-dimensional arrays.
The PHP function used is: array_multisort.
Idea: Get the field you need to sort as a one-dimensional array arr1, which will be used to sort the multi-dimensional array data later.
Here we mainly take a two-dimensional array as an example, and the same is true for multi-dimensional arrays Same idea.
$data = array(
array('price' => '500', 'count' => '40', 'level' => '1'),
array('price' => '600', 'count' => '30', 'level' => '2'),
array('price' => '650', 'count' => '20', 'level' => '3'),
array('price' => '700', 'count' => '10', 'level' => '4'),
);
Assuming that the flashback is based on price, we need to obtain the price field values, as a new one-dimensional array.
$arr1 = array_map(create_function('$n', 'return $n["price"];'), $data);
If the PHP version is greater than 5.5, you can directly use the array_column array operation method directly To get a certain field, you can also get it through foreach, but try to use built-in functions.
Then use array_multisort to process,
array_multisort($arr1,SORT_DESC,$data);//Sort of multi-dimensional arrays
We can print the final result of $data to see the result:

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
