In this paper, we describe the multi-dimensional array sorting algorithm implemented by PHP. Share to everyone for your reference, as follows:
Suddenly think of an interview question, a multidimensional array sort.
Cases:
<?php//has a multidimensional array of $ A = Array (' key1 ' =>940, ' key2 ' = ' blah '), array (' Key1 ' =>23, ' key2 ' = ') This '), array (' Key1 ' =>894, ' key2 ' = ' that ')), or//So how to sort key1 or key2, here you need to use Usort ($arr, ' myfunction ') function, it is the role of $arr using our custom method to sort, the use of the method can be viewed in the manual//1. Sort the values of Key1 function Asc_key1_sort ($x, $y) { //You can output it and see how it compares. Echo ' iteration: '. $x [' Key1 ']. ' vs '. $y [' Key1 ']; if ($x [' Key1 '] > $y [' key1 ']) { echo ' true<br/> '; return true; } ElseIf ($x [' Key1 '] < $y [' Key1 ']) { echo ' false<br/> '; return false; } else { echo ' 0 '; return 0;} } Sort Usort ($a, ' asc_key1_sort '); Var_dump ($a);//2. Sort Key2 characters Function asc_key2_sort ($x, $y) { // You can use the strcasecmp () function to sort echo ' iteration: '. $x [' Key2 ']. ' vs '. $y [' Key2 ']. ' <br/> '; return strcasecmp ($x [' Key2 '], $y [' Key2 ']);} Sort Usort ($a, ' asc_key2_sort '); Var_dump ($a);? >
Operation Result:
Iteration:23 vs 940falseiteration:894 vs 23trueiteration:940 vs 23trueiteration:894 vs 940falsearray (3) {[0]=> Array ( 2) {["Key1"]=> int () ["Key2"]=> string (4) "This"} [1]=> Array (2) {["Key1"]=> Int (894) ["Key2"]=> Strin G (4) "that"} [2]=> Array (2) {["Key1"]=> int (940) ["Key2"]=> string (4) "Blah"}} iteration:that vs Thisiteratio N:blah vs Thatarray (3) {[0]=> Array (2) {["Key1"]=> int (940) ["Key2"]=> string (4) "Blah"} [1]=> Array (2) {[ "Key1"]=> Int (894) ["Key2"]=> string (4) "that"} [2]=> Array (2) {["Key1"]=> Int (23°c) ["Key2"]=> string (4) " This "}}
What if I have a key value in my multidimensional array?
<?php//has a multidimensional array of $ A = Array (123 = = Array ( ' Key1 ' =>940, ' key2 ' = ' blah '), 349 = = Array (' key1 ' = The ' key2 ' = ' this '), the [' Key1 ' =>894, ' key2 ' = ' that '), and/or so how to sort key1 or Key2, This requires the use of the Usort ($arr, ' myfunction ') function, which is used to sort the $arr using our own custom methods, which can be used to view the manual//1. Sort the values of Key1 function Asc_key1_sort ( $x, $y) { //can output a bit to see how it compares the echo ' iteration: '. $x [' Key1 ']. ' vs '. $y [' Key1 ']; if ($x [' Key1 '] > $y [' key1 ']) { echo ' true<br/> '; return true; } ElseIf ($x [' Key1 '] < $y [' Key1 ']) { echo ' false<br/> '; return false; } else { echo ' 0 '; return 0;} } Sort Usort ($a, ' asc_key1_sort '); Var_dump ($a);//2. Sort Key2 characters Function asc_key2_sort ($x, $y) { // You can use the strcasecmp () function to sort echo ' iteration: '. $x [' Key2 ']. ' vs '. $y [' Key2 ']. ' <br/> '; return strcasecmp ($x [' Key2 '], $y [' Key2 ']);} Sort Usort ($a, ' asc_key2_sort '); Var_dump ($a);? >
Operation Result:
Iteration:23 vs 940falseiteration:894 vs 23trueiteration:940 vs 23trueiteration:894 vs 940falsearray (3) {[0]=> Array ( 2) {["Key1"]=> int () ["Key2"]=> string (4) "This"} [1]=> Array (2) {["Key1"]=> Int (894) ["Key2"]=> Strin G (4) "that"} [2]=> Array (2) {["Key1"]=> int (940) ["Key2"]=> string (4) "Blah"}} iteration:that vs Thisiteratio N:blah vs Thatarray (3) {[0]=> Array (2) {["Key1"]=> int (940) ["Key2"]=> string (4) "Blah"} [1]=> Array (2) {[ "Key1"]=> Int (894) ["Key2"]=> string (4) "that"} [2]=> Array (2) {["Key1"]=> Int (23°c) ["Key2"]=> string (4) " This "}}
Such sorting results are not retained for 123,349, 43. It's just a good time to usort()
change uasort
it!