Home > Backend Development > PHP Tutorial > php sort two dimensional array

php sort two dimensional array

WBOY
Release: 2016-07-25 08:58:13
Original
1076 people have browsed it
This article introduces an example of using the array_multisort function to sort two-dimensional arrays in PHP. Friends in need can refer to it.

Following the previous article: PHP two-dimensional array sorting custom function, today, we introduce another example of PHP two-dimensional array sorting. Sorting two-dimensional arrays in PHP is very simple, mainly using the array_multisort function.

Example:

<?php
/**
* php二维数组排序
* edit bbs.it-home.org
*/
    $data = array();
    $data[] = array('volume' => 67, 'edition' => 2);
    $data[] = array('volume' => 86, 'edition' => 1);
    $data[] = array('volume' => 85, 'edition' => 6);
    $data[] = array('volume' => 98, 'edition' => 2);
    $data[] = array('volume' => 86, 'edition' => 6);
    $data[] = array('volume' => 67, 'edition' => 7);

    // 取得列的列表
    foreach ($data as $key => $row)
    {
        $volume[$key]  = $row['volume'];
        $edition[$key] = $row['edition'];
    }

    array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);

    print_r($data);
?>
Copy after login

Output result:

Array ( [0] => Array ( [volume] => 98 [edition] => 2 )

[1] => Array ( [volume] => 86 [edition] => 1 )

[2] => Array ( [volume] => 86 [edition] => 6 )

[3] => Array ( [volume] => 85 [edition] => 6 )

[4] => Array ( [volume] => 67 [edition] => 2 )

[5] => Array ( [volume] => 67 [edition] => 7 ) )

Instructions: The parameters of the array_multisort function are very flexible. You can refer to the instructions in the PHP manual for in-depth study.

>>> For more information, please view the complete list of php array sorting methods



Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template