How PHP sorts a key value for a multidimensional array

Source: Internet
Author: User
Tags foreach arrays reset sort sorts

PHP multidimensional array sorting can be handled using the Array_multisort () function, which can be used to sort multiple arrays at once, or to sort multidimensional arrays based on one dimension or multidimensional, and the associated (string) key name remains unchanged. But the number key name will be indexed again

Sort order Flags:

Sort_asc-Sort by ascending order

Sort_desc-Sort in descending order

Sort Type Flags:

Sort_regular-Compare items in the usual way

Sort_numeric-Compare items by numerical comparison

Sort_string-Comparing items by string

Example 1. Sort on multiple arrays

The code is as follows

<?php
$ar 1 = Array ("a");
$ar 2 = Array (1, 3, "2", 1);
Array_multisort ($ar 1, $ar 2);

Var_dump ($ar 1);
Var_dump ($ar 2);
?>

After sorting in this example, the first array will contain "ten", "a", 100,100. The second array will contain 1, 1, "2", and 3. The sequence of items in the second array is exactly the same as the corresponding items in the first array (100 and 100).

But these can not be specified by the key value ranking, I have sorted out a few function algorithms are good.

Sample code

The code is as follows

/**
* Sort multiple-bit arrays
* @param $multi _array Array
* @param $sort _key need to pass in the key name
* @param $sort Sort Type
*/
function Multi_array_sort ($multi _array, $sort _key, $sort = Sort_desc) {
if (Is_array ($multi _array)) {
foreach ($multi _array as $row _array) {
if (Is_array ($row _array)) {
$key _array[] = $row _array[$sort _key];
} else {
return FALSE;
}
}
} else {
return FALSE;
}
Array_multisort ($key _array, $sort, $multi _array);
return $multi _array;
}

Example Two

The code is 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);

foreach ($keysvalue as $k => $v) {

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

return $new _array;
}

Example 3

  code is as follows &nbs P;

/*
Function: Two-dimensional array sorted by specified key value
author:www.111cn.net
*/
F Unction Array_sort ($array, $keys, $type = ' asc ') {
if!isset ($array) | |!is_array ($array) | | empty ($array)) {
Return ";
}
if (!isset ($keys) | | | trim ($keys) = = ') {
return ';
}
if (!isset ($type) | | | $type = = ' | |!in_array (strtolower ($type), Array (' ASC ', ' desc ')) {
return ';
}
$keysvalue =array ();
foreach ($array as $key => $val) {
$val [$keys] = Str_replace ('-', ', ', $val [$keys]);
$val [$keys] = Str_replace (', ', $val [$keys]);
$val [$keys] = Str_replace (': ', ', $val [$keys]);
$keysvalue [] = $val [$keys];
}
Asort ($keysvalue);//key value sort
Reset ($keysvalue);//pointer points to array first
foreach ($keysvalue as $key => $vals) {
$keysort [] = $key;
}
$keysvalue = Array ();
$count =count ($keysorT);
if (Strtolower ($type)!= ' ASC ') {
for ($i = $count-1; $i >=0; $i-) {
$keysvalue [] = $array [$keysort [$i]];

}else{
for ($i =0; $i < $count; $i + +) {
$keysvalue [] = $array [$keysort [$i]];
}
}
return $keysvalue;
}

For example, there is an array such as the following:

The code is as follows

$array =array (
0=>array (' id ' =>8, ' username ' => ' phpernote '),
1=>array (' id ' =>9, ' username ' => ' com '),
2=>array (' id ' =>5, ' username ' => ' www ')
);

Now you need to sort this two-dimensional array in ascending ID, then:

Array_sort ($array, ' id ', ' ASC ');

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.