PHP custom functions and array sorting examples

Source: Internet
Author: User
Tags array sort
about sort generally we are all sorted by database or NoSQL (Eg:redis) and then output to the program directly, but sometimes we need to sort the arrays directly via PHP, and the most used data in PHP is object and array. But the more processing is the array, because there are very richBuilt-in functions libraries (in fact objects can be understood to be an array in some way), and these libraries can largely help us implement certain functions. The commonly used system functions are sort, asort, Arsort, Ksort, Krsort, and so on, here I mainly say to two-dimensional array of the sort, both ways:

First, with PHP self-array_multisort function sort

<?php  $data = Array ();    $data [] = Array (' volume ' = +, ' edition ' = 2);    $data [] = Array (' volume ' = +, ' edition ' = 1);    $data [] = Array (' volume ' = =, ' edition ' = 6);    $data [] = Array (' volume ' = = 98, ' edition ' = 2);    $data [] = Array (' volume ' = +, ' edition ' = 6);    $data [] = Array (' volume ' = +, ' edition ' = 7);    Get the list of columns    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);? >

Output Result:

Array (    [0] = = Array        (            [Volume] = 98            [Edition] = 2        )    [1] = = Array        (            [ Volume] [            Edition] = 1        )    [2] = = Array        (            [Volume] = [            Edition] = 6< c14/>)    [3] = = Array        (            [Volume] =            [Edition] = 6        )    [4] = = Array        (            [Volume] =            [Edition] + 2        )    [5] = =        Array            ([volume] = [            Edition] = 7        ))

II. Ordering of custom functions 1

<?php $data = Array ();    $data [] = Array (' volume ' = +, ' edition ' = 2);    $data [] = Array (' volume ' = +, ' edition ' = 1);    $data [] = Array (' volume ' = =, ' edition ' = 6);    $data [] = Array (' volume ' = = 98, ' edition ' = 2);    $data [] = Array (' volume ' = +, ' edition ' = 6);    $data [] = Array (' volume ' = +, ' edition ' = 7);        Get the list of columns foreach ($data as $key + = $row) {$volume [$key] = $row [' volume '];    $edition [$key] = $row [' Edition '];    } $ret = ArraySort ($data, ' volume ', ' desc ');    Print_r ($ret);     /** * @desc arraysort php Two-dimensional array sort sorted by the specified key * @param array $arr will be sorted * @param string $keys Specify the sorted key * @param string $type sort type ASC | DESC * @return Array */function ArraySort ($arr, $keys, $type = ' asc ') {$keysvalue = $new _array = array        ();        foreach ($arr as $k = + $v) {$keysvalue [$k] = $v [$keys]; } $type = = ' ASC '? Asort ($keysvalue): ARSort ($keysvalue);        Reset ($keysvalue);        foreach ($keysvalue as $k = + $v) {$new _array[$k] = $arr [$k];    } return $new _array; }?>

Output Result:

Array (  [3] = = Array     (           [Volume] = 98           [Edition] = 2        )    [4] = = Array        (            [ Volume] [            Edition] = 6        )    [1] = = Array        (            [Volume] = [            Edition] = 1< c14/>)    [2] = = Array        (            [Volume] =            [Edition] = 6        )    [5] = = Array        (            [Volume] =            [Edition] + 7        )    [0] = =        Array            ([volume] = [            Edition] = 2        ))

One of the differences between this custom function and the system function is that the custom function only supports sorting on a key, and it needs to be executed multiple times to support the ordering of multiple keys; While the system function Array_multisort can be one-time to multiple keys and can specify more than one collation, the system function is quite powerful, recommend the use of system functions, after all, is the C bottom-level implementation, here is just an example of how to sort the array by a custom function, Of course, this custom function can also continue to expand to support more collations. In the rankings, leaderboards, scores and other scenes used in a lot of.

III. Ordering of custom functions 2

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

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), foreach ($keysvalue as $k = = $v) {  $new _array[$k] = $arr [$k];} 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:

$array = Array (' name ' = ' phone ', ' brand ' = ' Nokia ', ' Price ' =>1050), Array (' name ' = ' "laptop ', ' brand ' = ') ' Lenovo ', ' Price ' =>4300), Array (' name ' = = ' razor ', ' brand ' = ' Philips ', ' Price ' =>3100), Array (' name ' = ' "' Treadmill ', ' Brand ' = ' three and the Pine stone ', ' price ' =>4900, Array (' name ' = ' watch ', ' brand ' = ' casio ', ' Price ' =>960), Array (' name ' = = ' LCD tv ', ' Brand ' + ' sony ', ' Price ' =>6299, Array (' name ' = ' laser printer ', ' brand ' = ' hp ', ' Price ' =>1200)); $ Shoppinglist = Array_sort ($array, ' price ');p Rint_r ($ShoppingList);

The

Above is the $array of the two-dimensional array according to ' Price ' from low to high sort.

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.