Php multi-dimensional array sorting method

Source: Internet
Author: User
The code is as follows: Copy code

// Sort multiple arrays

$ Ar1 = array ("ten", 100,100, "");
$ Ar2 = array (1, 3, "2", 1 );
Array_multisort ($ ar1, $ ar2 );

Var_dump ($ ar1 );
Var_dump ($ ar2 );

// In this example, after sorting, the first array will contain "10", "a", 100,100. The second array will contain 1, 1, "2", 3. The project order in the second array is exactly the same as that in the first array (100 and 100.

$ Ar = array (
Array ("10", 11,100,100, ""),
Array (1, 2, "2", 3, 1)
);
Array_multisort ($ ar [0], sort_asc, sort_string,
$ Ar [1], sort_numeric, sort_desc );
Var_dump ($ ar );

/*
Sort order mark:
◆ Sort_asc-sort by ascending order
◆ Sort_desc-sort by descent

Sorting type flag:
◆ Sort_regular-compare projects according to common methods
◆ Sort_numeric-compare projects by numerical values
◆ Sort_string-compare items by string

You cannot specify two Sorting flags of the same type after each array. The sorting flag specified after each array is only valid for this array-the default values are sort_asc and sort_regular.

*/

Function cmp ($ a, $ B)
{
Return strcmp ($ a ["fruit"], $ B ["fruit"]);
}

$ Fruits [0] ["fruit"] = "lemons ";
$ Fruits [1] ["fruit"] = "apples ";
$ Fruits [2] ["fruit"] = "grapes ";

Usort ($ fruits, "cmp ");

While (list ($ key, $ value) = each ($ fruits )){
Echo "$ fruits [$ key]:". $ value ["fruit"]. "";
}

// Check out a complete

$ Data = array (
Array (
'Name' => 'Alice ',
'Key' => '2fc4ab3d639e5400efdfc73bc27e83f1 ',
'Age' => 20
),
Array (
'Name' => 'loudia ',
'Key' => '831c2b79c1f19af39c7e3321e11e5f5e ',
'Age' => 18
),
Array (
'Name' => 'Beatrice ',
'Key' => '6f8512a2066b8f35a27a495ce1228c76 ',
'Age' = & gt; 100
),
Array (
'Name' => 'Denise ',
'Age' => 25,
'Key' => 'ef6de3b178bf9f69a9fef72e4ee7bbe9'
 )
);

$ Arr1 = sortdata ($ data, "name", "asc ");
Echo "<pre> ";
Var_dump ($ arr1 );
Echo "</pre> ";

$ Arr2 = sortdata ($ data, "age", "desc ");
Echo "<pre> ";
Var_dump ($ arr2 );
Echo "</pre> ";


Function sortdata ($ data, $ col, $ order ){
If (count ($ data) $ temp_array [key ($ data)] = array_shift ($ data );

Foreach ($ data as $ key => $ val ){
$ Offset = 0;
$ Found = false;
Foreach ($ temp_array as $ tmp_key => $ tmp_val ){
If (! $ Found and strtolower ($ val [$ col])> strtolower ($ tmp_val [$ col]) {
$ Temp_array = array_merge (array) array_slice ($ temp_array, 0, $ offset ),
Array ($ key => $ val ),
Array_slice ($ temp_array, $ offset)
);
$ Found = true;
   }
$ Offset ++;
  }
If (! $ Found ){
$ Temp_array = array_merge ($ temp_array, array ($ key => $ val ));
   }
 }
If (strtolower ($ order) = "asc "){
$ Array = array_reverse ($ temp_array );
} Else {
$ Array = $ temp_array;
 }
Return $ array;
}

?>

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.