PHP about Array

Source: Internet
Author: User
Tags foreach arrays copy empty sort
First, see the simplest situation. There are two arrays:
Copy CodeThe code is as follows:
$arr 1 = array (1,9,5);
$arr 2 = array (6,2,4);
Array_multisort ($arr 1, $arr 2);
Print_r ($arr 1); The order to get is 1,5,9
Print_r ($arr 2); The order to get is 6,4,2

I estimate that the values of the two arrays are always corresponding: 1 corresponds to the 6,9 corresponding 2,5 4.
Let's add one more array and see what happens:
Copy CodeThe code is as follows:
$arr 1 = array (1,9,5);
$arr 2 = array (6,2,4);
$arr 3 = array (3,7,8);
Array_multisort ($arr 1, $arr 2, $arr 3);

Looking at the results, 1 corresponds to 6 for 3 from start to finish, as are the other items. This correspondence is the so-called "preserve the original key-name association" in the Manual.
Alternatively, you can think of each array as a column of database tables. The corresponding 1,6,3 is a data row, 9,2,7 is another data row ...
Array_multisort the first array (imagined as a column), sorted by the second array (column) if the value of the first array (column) is the same.
You can use the following procedure to test:
Copy CodeThe code is as follows:
$arr 1 = array (1,9,5,9);
$arr 2 = array (6,2,4,1);
$arr 3 = array (3,7,8,0);
Array_multisort ($arr 1, $arr 2, $arr 3);

It can be imagined that the result of this $arr3 is (3,8,0,7).
Second, then explain the parameters of Array_multisort. The parameters of this function are very flexible. The simplest scenario is as an argument with 1 or n arrays as shown above, and it is important to note that the number of items in each array is the same, otherwise warning will result in a sort failure.
Like this array_multisort ($arr 1, $arr 2, $arr 3); By default, all arrays are in ascending order, and if you want to $arr2 descending, and compare them as strings, write:
Array_multisort ($arr 1, $arr 2, Sort_desc, sort_string, $arr 3);
Each array can be followed by a sort order flag or a sort type flag, or two flags at the same time. However, each sort flag can only appear after each array.
Details are as follows:
Sort order Flags:

SORT_ASC-Sort in ascending order (default)
Sort_desc-Sort in descending order

Sort Type Flags:

Sort_regular-Compare items in the usual way (default)
Sort_numeric-Compare items by numerical comparison
Sort_string-Comparing items by string

third, the last is Array_multisort have any practical effect.
We usually have some multidimensional arrays that need sorting:
$guys = Array
(
[0] => Array
(
[Name] => Jake
[Score] => 80
[Grade] => A
)
[1] => Array
(
[Name] => Jin
[Score] => 70
[Grade] => A
)
[2] => Array
(
[Name] => John
[Score] => 80
[Grade] => A
)
[3] => Array
(
[Name] => Ben
[Score] => 20
[Grade] => B
)
)
For example, we want to arrange by the order of the results, if the results are the same in ascending order of the name. Then we need to get two more arrays in the order of $guys: $scores = Array (80,70,80,20), $names = Array (' Jake ', ' Jin ', ' John ', ' Ben '), and then Array_multisort ($scores, Sort_desc, $names, $guys); Can you be more flexible, you want to order each time to get some other array out? In fact, in the Qeephp Helper_array class has been encapsulated very well, the following is its two methods, the need to modify the person you can use:
Copy CodeThe code is as follows:
/**
* Sort the array according to the specified key
*
Usage
* @code PHP
* $rows = Array (
* Array (' ID ' => 1, ' value ' => ' 1-1 ', ' parent ' => 1),
* Array (' ID ' => 2, ' value ' => ' 2-1 ', ' parent ' => 1),
* Array (' ID ' => 3, ' value ' => ' 3-1 ', ' parent ' => 1),
* Array (' ID ' => 4, ' value ' => ' 4-1 ', ' parent ' => 2),
* Array (' ID ' => 5, ' value ' => ' 5-1 ', ' parent ' => 2),
* Array (' ID ' => 6, ' value ' => ' 6-1 ', ' Parent ' => 3),
* );
*
* $rows = Helper_array::sortbycol ($rows, ' id ', SORT_DESC);
* Dump ($rows);
*//The output result is:
*//Array (
*//Array (' ID ' => 6, ' value ' => ' 6-1 ', ' Parent ' => 3),
*//Array (' ID ' => 5, ' value ' => ' 5-1 ', ' parent ' => 2),
*//Array (' ID ' => 4, ' value ' => ' 4-1 ', ' parent ' => 2),
*//Array (' ID ' => 3, ' value ' => ' 3-1 ', ' parent ' => 1),
*//Array (' ID ' => 2, ' value ' => ' 2-1 ', ' parent ' => 1),
*//Array (' ID ' => 1, ' value ' => ' 1-1 ', ' parent ' => 1),
* // )
* @endcode
*
* @param array $array to sort
* @param string $keyname the sorted key
* @param int $dir sort Direction
*
* Array sorted @return Array
*/
static function Sortbycol ($array, $keyname, $dir = SORT_ASC)
{
Return Self::sortbymulticols ($array, Array ($keyname => $dir));
} /**
* Sort a two-dimensional array by multiple columns, similar to the order by in SQL statements
*
Usage
* @code PHP
* $rows = Helper_array::sortbymulticols ($rows, Array (
* ' Parent ' => SORT_ASC,
* ' name ' => Sort_desc,
* ));
* @endcode
*
* @param array $rowset to sort
* @param array $args sort keys
*
* Array sorted @return Array
*/
static function Sortbymulticols ($rowset, $args)
{
$sortArray = Array ();
$sortRule = ';
foreach ($args as $sortField => $sortDir)
{
foreach ($rowset as $offset => $row)
{
$sortArray [$sortField] [$offset] = $row [$sortField];
}
$sortRule. = ' $sortArray [\ '. $sortField. '\'], ' . $sortDir. ', ';
}
if (empty ($sortArray) empty ($sortRule)) {return $rowset;}
Eval (' Array_multisort $sortRule. ' $rowset);
return $rowset;
}



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.