Example of multi-element ordering for PHP two-dimensional arrays

Source: Internet
Author: User

Write a two-dimensional array of multiple-element ordering, multiple-element sorting similar to the order by sort,date in SQL, the following code considers performance issues and involves sorting only 2 elements.

Code area:

function Multiplearraysort ($old _arr, $arr 1, $arr 2) {
Sort the first element
$old _arr = Array_sort ($old _arr, $arr 1[0], $arr 1[1]);
$temp _array = $new _temp_array = Array ();
foreach ($old _arr as $k => $v) {
$key _count = count ($old _arr)-1;
Equal number of first elements, put in temporary array
if ($v [$arr 1[0]] = = = $old _arr[$k -1][$arr 1[0]] | | empty ($temp _array)) {
$temp _array[$k] = $v;
}
if (($v [$arr 1[0]]!= $old _arr[$k -1][$arr 1[0]] &&!empty ($temp _array)) | | $key _count = = $k) {
Add the latest element
$temp _array[$k] = $v;
Array is greater than 1 o'clock, sorting processing
if (count ($temp _array) >1) {
if ($key _count!= $k) {
Deletes the last one (with the previous inequality element)
unset ($temp _array[$k]);
}
Not last or last the same, sort
if ($key _count!= $k | | $v [$arr 1[0]] = = $old _arr[$k -1][$arr 1[0]]) {
Sort a second element
$temp _array = Array_sort ($temp _array, $arr 2[0], $arr 2[1]);
}
Sort after the sorted array, insert the original array to sort the position before
$count = count ($new _temp_array) ==0? 0:count ($new _temp_array);
foreach ($temp _array as $val _t) {
$new _temp_array[$count] = $val _t;
$count + +;
}
Empty the original array after writing the new array
$temp _array = Array ();
Add the latest element
$temp _array[$k] = $v;
}
}
}
return $new _temp_array;
}
Sort by array elements
function Array_sort ($arr, $keys, $type = ' asc ') {
$keysvalue = $new _array = Array ();
Gets the element value and generates a new array
foreach ($arr as $k => $v) {
$keysvalue [$k] = $v [$keys];
}
Sort, default to positive sequence
if ($type = = ' asc ') {
Asort ($keysvalue);
}else{
Arsort ($keysvalue);
}
Reset ($keysvalue);
$count _s = 0;
Recovery key value
foreach ($keysvalue as $k => $v) {
$new _array[$count _s] = $arr [$k];
$count _s++;
}
return $new _array;
}

Call Method:


$old _arr = Array (
Array (' ID ' =>1, ' sort ' =>1, ' date ' => ' 1405648791 '),
Array (' ID ' =>2, ' sort ' =>1, ' date ' => ' 1405649791 '),
Array (' ID ' =>3, ' sort ' =>2, ' date ' => ' 1405647791 '),
);
$arr 1 = Array (' sort ', ' desc ');
$arr 2 = Array (' Date ', ' desc ');
$NEWARR = Multiplearraysort ($old _arr, $arr 1, $arr 2);
Var_dump ($NEWARR);

Print results:

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.