Deep parsing _php techniques for PHP array_multisort () functions

Source: Internet
Author: User

First, see the simplest case. There are two arrays:
$arr 1 = array (1,9,5);
$arr 2 = array (6,2,4);
Array_multisort ($arr 1, $arr 2);
Print_r ($arr 1);//The resulting order is 1,5,9
Print_r ($arr 2);//The resulting order is 6,4,2
I estimate that the values of the two arrays correspond from start to finish: 1 corresponds to 6,9 corresponding 2,5 to 4.
Let's add one more array to see what happens:
$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);
To view 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 a database table. The corresponding 1,6,3 is a data row, 9,2,7 is another data row ...
Array_multisort is sorted by the first array (imagined as a column), sorted by the second array (column) if the value of the first array (column) is the same. The
can be tested with the following program:
$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); The
can imagine the result of the $ARR3 here 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:

Copy Code code as follows:

$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);It's okay.
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 Code code 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.