Analysis of Multi Dimensional Array Sorting Algorithm Based on PHP
- 2021-09-11 19:47:18
- OfStack
This paper describes the multi-dimensional array sorting algorithm implemented by PHP. Share it for your reference, as follows:
Suddenly, I remembered an interview question and sorted a multidimensional array.
Example:
<?php
// Have 1 Multidimensional arrays
$a = array(
array('key1'=>940, 'key2'=>'blah'),
array('key1'=>23, 'key2'=>'this'),
array('key1'=>894, 'key2'=>'that')
);
// So what's right key1 Or key2 What about sorting , You need to use it here usort($arr, 'myfunction') Function, its function is to $arr Use our custom method to sort, and you can see the manual for specific use
//1. Right key1 Sorts the values of
function asc_key1_sort($x, $y) {
// Can output 1 Let's see how it is compared
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. Right key2 Characters are sorted
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);
?>
Run results:
Iteration:23 vs 940false
Iteration:894 vs 23true
Iteration:940 vs 23true
Iteration:894 vs 940false
array(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 this
Iteration:blah vs that
array(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 multidimensional array also has key values?
<?php
// Have 1 Multidimensional arrays
$a = array(
123 => array('key1'=>940, 'key2'=>'blah'),
349 => array('key1'=>23, 'key2'=>'this'),
43 => array('key1'=>894, 'key2'=>'that')
);
// So what's right key1 Or key2 What about sorting , You need to use it here usort($arr, 'myfunction') Function, its function is to $arr Use our custom method to sort, and you can see the manual for specific use
//1. Right key1 Sorts the values of
function asc_key1_sort($x, $y) {
// Can output 1 Let's see how it is compared
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. Right key2 Characters are sorted
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);
?>
Run results:
Iteration:23 vs 940false
Iteration:894 vs 23true
Iteration:940 vs 23true
Iteration:894 vs 940false
array(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 this
Iteration:blah vs that
array(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" } }
This sort will not retain 123, 349, 43. At this time, just put
usort()
Change to
uasort
That's good!
PS: Here we recommend another demonstration tool about sorting for your reference:
Online animation demonstrates insert/select/bubble/merge/hill/quick sort algorithm process tool:
http://tools.ofstack.com/aideddesign/paixu_ys
For more readers interested in PHP related contents, please check the topics of this site: "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary", "php String (string) Usage Summary", "PHP Array (Array) Operation Skills Complete Book", "PHP Common Traversal Algorithms and Skills Summary" and "PHP Mathematical Operation Skills Summary"
I hope this article is helpful to everyone's PHP programming.