Home php教程 PHP开发 PHP two-dimensional array sorting method (array_multisort usort)

PHP two-dimensional array sorting method (array_multisort usort)

Jan 06, 2017 pm 04:48 PM

For example, an array like the following:

$users = array(
    array('name' => 'tom', 'age' => 20)
    , array('name' => 'anny', 'age' => 18)
    , array('name' => 'jack', 'age' => 22)
);
Copy after login

I hope it can be sorted by age from small to large. The author has sorted out two methods and shared them with you.

1. Use array_multisort

Using this method will be more troublesome. You need to extract the age and store it in a one-dimensional array, and then arrange it in ascending order by age. The specific code is as follows:

$ages = array();
foreach ($users as $user) {
    $ages[] = $user['age'];
}
array_multisort($ages, SORT_ASC, $users);
Copy after login

After execution, $users is a sorted array, which can be printed out to see. If you need to sort by age in ascending order first, and then by name in ascending order, the method is the same as above, that is, extract an additional name array. The final sorting method is called like this:

array_multisort($ages, SORT_ASC, $names, SORT_ASC, $users);
Copy after login

2. Use usort

Use The biggest advantage of this method is that you can customize some more complex sorting methods. For example, sort in descending order according to the length of the name:

usort($users, function($a, $b) {
            $al = strlen($a['name']);
            $bl = strlen($b['name']);
            if ($al == $bl)
                return 0;
            return ($al > $bl) ? -1 : 1;
        });
Copy after login

Anonymous function is used here, and it can be extracted separately if necessary. Among them, $a and $b can be understood as elements under the $users array. You can directly index the name value, calculate the length, and then compare the lengths.

The author prefers the second method because there is no step to extract the sorted content into a one-dimensional array, and the sorting method is more flexible.

For more articles related to PHP two-dimensional array sorting method (array_multisort usort), please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)