PHP multidimensional array Specifies sample code for multi-field ordering _php instance

Source: Internet
Author: User
Tags array sort php example php sms verification
This article mainly introduces the PHP multidimensional array to specify a multi-field ordering example code, small series feel very good, and now share to everyone, but also for everyone to do a reference. Let's take a look at it with a little knitting.

Introducing the Array_multisort method

array_multisort-to sort multiple arrays or multidimensional arrays. The instructions in its PHP manual are as follows:

Copy the Code code as follows:

BOOL Array_multisort (array & $arr [, Mixed $arg = Sort_asc [, Mixed $arg = Sort_regular [, mixed $ ...])

Parameters

Arr
An array to sort on.

Arg
Each of the following parameters can be another array or the option parameter for the previous array sort flag: Sort_asc, Sort_desc,sort_regular, Sort_numeric, sort_string.
...
Additional Arg ' s.

To implement a field ordering of a specified multidimensional array first

This requires that the array be sorted based on one of the fields, and the arrays are assumed as follows:


  $array = Array (      0=>array (' id ' =>8, ' name ' = ' Tom '),      1=>array (' id ' =>9, ' name ' = ' Peter '),      2=>array (' id ' =>5, ' name ' = ' Jack ')  );


We want to sort by the ID value of the two-dimensional array, the converted array format is as follows:


  $array = Array (      0=>array (' id ' =>5, ' name ' = ' Jack ')      1=>array (' id ' =>8, ' name ' = ' Tom '),      2=>array (' id ' =>9, ' name ' = ' Peter ')  );


To complete the above conversion, you need to use the Array_multisort function described above, as follows:


  Function Sortarrbyonefield (& $array, $field, $desc = False) {    $FIELDARR = array ();    foreach ($array as $k = + $v) {     $FIELDARR [$k] = $v [$field];    }    $sort = $desc = = False? Sort_asc:sort_desc;    Array_multisort ($FIELDARR, $sort, $array);  }


By storing the $field of the individual arrays of the array to be sorted in an array fieldarr, participate in the ordering in the incoming array_multisort. Where the value of the field array is as follows:


Array (0=>8,1=>9,2=>5)


When Array_multisort is passed in, it is equivalent to sorting the $field one-dimensional array, and then rebuilding the incoming sorted array based on the sorted key.

To implement specifying multiple field ordering for a multidimensional array

The above example explains how to implement a multidimensional array to specify a field sort, but how do you think if you want to implement the ordering of multiple fields in a specified sequence?
How many fields are several? 2, 3 or more, so this uncertain factor needs to be ruled out.

Let's take a look at 2. Specify a scenario for sorting 2 fields:


  $arr = Array ('    0 ' = = Array (      ' id ' = = 3, ' age ' = '     ),    ' 1 ' = = Array (      ' id ' = = 5,      ' Age ' = ' +    ', ' 2 ' = = Array (      ' id ' = = 4,      ' age ' = '    ),    ' 3 ' = = Array (      ' ID ' = 3,      ' age ' = +    )   ;  foreach ($arr as $key = + $row) {    $id [$key] = $row [' id '];    $age [$key] = $row [' age '];  }  Array_multisort ($id, SORT_ASC, $age, Sort_desc, $arr);  Print_r ($arr);  Result:array ([' 0]=>array ' [' id ']=>3 [' age ']=>78] [1]=>array ([' ID ']=>3 [' age ']=>27] [2]=>array ([' ID ']=>4 [' Age ']=>44) [3]=>array ([' ID ']=>5 [' age ']=>50)]


Refactoring the above code, as long as the PHP Func_get_args function, to dynamically obtain the incoming value, can not only solve the problem of the number of multi-field uncertainties. The implementation is as follows:


  $array 1 = Array (        0=>array (' id ' =>8, ' name ' = ' Apple ', ' age ' = +),        1=>array (' id ' =>8, ' name ' = > ' Bed ', ' age ' =>17,        2=>array (' id ' =>5, ' name ' = ' Cos ', ' age ' =>16),        3=>array (' id ' =>5 , ' name ' = ' Cos ', ' age ' =>14)  ;  function Sortarrbymanyfield () {    $args = Func_get_args ();    if (empty ($args)) {      return null;    }    $arr = Array_shift ($args);    if (!is_array ($arr)) {      throw new Exception ("the first parameter is not an array");    }    foreach ($args as $key = + $field) {      if (is_string ($field)) {        $temp = array ();        foreach ($arr as $index = + $val) {          $temp [$index] = $val [$field];        }        $args [$key] = $temp;      }    }    $args [] = & $arr;//reference value    call_user_func_array (' Array_multisort ', $args);    Return Array_pop ($args);  }  $arr = Sortarrbymanyfield ($array 1, ' id ', SORT_ASC, ' name ', Sort_asc, ' age ', sort_desc);  Print_r ($arr);


The results of the operation are as follows:

Array (4) {
[0]=>array (3) {
["ID"]=>int (5)
["Name"]=>string (3) "Cos"
["Age"]=>int (16)
}
[1]=>array (3) {
["ID"]=>int (5)
["Name"]=>string (3) "Cos"
["Age"]=>int (14)
}
[2]=>array (3) {
["id"]=>int (8)
["Name"]=>string (5) "Apple"
["Age"]=>int (18)
}
[3]=>array (3) {
["id"]=>int (8)
["Name"]=>string (3) "Bed"
["Age"]=>int (17)
}
}

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support PHP Chinese network.

Articles you may be interested in:

PHP implementation of multi-image upload and single-image upload function php example

PHP implementation name sorted by initials class and method (instance code) PHP instance

PHP SMS Verification Code implementation process detailed PHP example

Related Article

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.