PHP instance sharing two-dimensional array sorting _php tutorial

Source: Internet
Author: User
PHP One-dimensional arrays can be sorted by sort (), Asort (), Arsort () and other functions;

The ordering of PHP two-dimensional arrays needs to be customized.

The following function is to sort a given two-dimensional array according to the specified key value, and first look at the function definition:

Copy the Code code as follows:
function Array_sort ($arr, $keys, $type = ' asc ') {
$keysvalue = $new _array = Array ();
foreach ($arr as $k = = $v) {
$keysvalue [$k] = $v [$keys];
}
if ($type = = ' asc ') {
Asort ($keysvalue);
}else{
Arsort ($keysvalue);
}
Reset ($keysvalue);

$index = 0;//save subscript unchanged with $k, subscript starting from 0 with $index;
foreach ($keysvalue as $k = = $v) {


$new _array[$index] = $arr [$k];

$index + +;
}
return $new _array;
}

It can sort the two-dimensional arrays by the specified key values, or you can specify ascending or descending sorting (the default is ascending), and the usage example:

Copy the Code code as follows:
$array = Array (
Array (' name ' = ' Js ', ' date ' = ' 2014-05-01 '),
Array (' name ' = = ' Sh ', ' date ' = ' 2014-04-30 '),
Array (' name ' = ' Bj ', ' date ' = ' 2014-05-02 ')
);

$arrayList = Array_sort ($array, ' Date ');
Print_r ($arrayList);

http://www.bkjia.com/PHPjc/770581.html www.bkjia.com true http://www.bkjia.com/PHPjc/770581.html techarticle php One-dimensional arrays can be sorted by sort (), Asort (), Arsort (), and the ordering of PHP two-dimensional arrays needs to be customized. The following function is for a given two-dimensional array to follow the specified key value into ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.