Multi-dimensional array Sorting Algorithm Analysis and multi-dimensional algorithm analysis implemented by PHP
This article describes the multi-dimensional array sorting algorithm implemented by PHP. We will share this with you for your reference. The details are as follows:
I suddenly remembered a interview question and ordered a multi-dimensional array.
Example:
<? Php // There is a multi-dimensional array $ a = array ('key1' => 940, 'key2' => 'blah'), array ('key1' => 23, 'key2' => 'this'), array ('key1' => 894, 'key2' => 'that ')); // how can we sort key1 or key2? Here we need to use the usort ($ arr, 'myfunction') function, it is used to sort $ arr by our custom method. For details, refer to the Manual // 1. sort the values of key1. function asc_key1_sort ($ x, $ y) {// You can output the echo 'iteration :'. $ x ['key1']. ''. $ 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 the key2 characters. function asc_key2_sort ($ x, $ y) {// you can use the strcasecmp () function to sort the key2 characters. echo 'iteration :'. $ x ['key2']. ''. $ y ['key2']. '<br/>'; return strcasecmp ($ x ['key2'], $ y ['key2']);} // sort usort ($, 'asc _ key2_sort '); var_dump ($ a);?>
Running result:
Iteration:23 vs 940falseIteration:894 vs 23trueIteration:940 vs 23trueIteration:894 vs 940falsearray(3) { [0]=> array(2) { ["key1"]=> int(23) ["key2"]=> string(4) "this" } [1]=> array(2) { ["key1"]=> int(894) ["key2"]=> string(4) "that" } [2]=> array(2) { ["key1"]=> int(940) ["key2"]=> string(4) "blah" } } Iteration:that vs thisIteration: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) ["key2"]=> string(4) "this" } }
What if my multi-dimensional array also has a key value?
<? Php // There is a multi-dimensional array $ a = array (123 => array ('key1' => 940, 'key2' => 'blah '), 349 => array ('key1' => 23, 'key2' => 'C'), 43 => array ('key1' => 894, 'key2' => 'that'); // how can we sort key1 or key2? Here we need to use the usort ($ arr, 'myfunction') function, it is used to sort $ arr by our custom method. For details, refer to the Manual // 1. sort the values of key1. function asc_key1_sort ($ x, $ y) {// You can output the echo 'iteration :'. $ x ['key1']. ''. $ 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 ($ ); // 2. sort the key2 characters. function asc_key2_sort ($ x, $ y) {// you can use the strcasecmp () function to sort the key2 characters. echo 'iteration :'. $ x ['key2']. ''. $ y ['key2']. '<br/>'; return strcasecmp ($ x ['key2'], $ y ['key2']);} // sort usort ($, 'asc _ key2_sort '); var_dump ($ a);?>
Running result:
Iteration:23 vs 940falseIteration:894 vs 23trueIteration:940 vs 23trueIteration:894 vs 940falsearray(3) { [0]=> array(2) { ["key1"]=> int(23) ["key2"]=> string(4) "this" } [1]=> array(2) { ["key1"]=> int(894) ["key2"]=> string(4) "that" } [2]=> array(2) { ["key1"]=> int(940) ["key2"]=> string(4) "blah" } } Iteration:that vs thisIteration: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) ["key2"]=> string(4) "this" } }
Such sorting results do not retain 123,349, 43. In this caseusort()ChangeuasortThat's all!
PS: here we recommend a demo tool for sorting for your reference:
Online animation demo insert/select/bubble/merge/hill/Quick Sort Algorithm process tool:
Http://tools.jb51.net/aideddesign/paixu_ys