This article illustrates the simple way to implement multidimensional array sorting in PHP. Share to everyone for your reference, specific as follows:
Before doing a function, you have to put the data into a two-dimensional array and sort, and then the Internet to find solutions to the idea,
The Array_multisort function is used, and the Array_multisort () function sorts multiple arrays or multidimensional arrays
Let's take a look at an example
<?php
$data =array (
0=>array (' One ' =>34, ' two ' => ' d '),
1=>array (' One ' =>45, ' two ' => ' E '),
2=>array (' One ' =>47, ' two ' => ' H '),
3=>array (' One ' =>12, ' two ' => ' C '),
4=> Array (' One ' =>15, ' two ' => ' W '),
5=>array (' One ' =>85, ' two ' => ' R '),
);
foreach ($data as $val) {
$key _arrays[]= $val [' one '];
}
Array_multisort ($key _arrays,sort_asc,sort_numeric, $data);
Var_dump ($data);
Output Result: Key value one sort, as follows:
Array
0 =>
array
' one ' => int
' two ' => string ' C ' (length=1)
1 =>
array
' One ' => int
' two ' => string ' W ' (length=1)
2 =>
array ' one
' => int
' two ' => St Ring ' d ' (length=1)
3 =>
array
' one ' => int
' two ' => string ' e ' (length=1)
4 =>
array
' one ' => int
' two ' => string ' h ' (length=1)
5 =>
array ' one
' => int 85< c24/> ' two ' => string ' R ' (length=1)
PHP Multidimensional Array sorting
The following are encapsulated into functions for easy use
function My_array_multisort ($data, $sort _order_field, $sort _order=sort_asc, $sort _type=sort_numeric) {
foreach ($data as $val) {
$key _arrays[]= $val [$sort _order_field];
}
Array_multisort ($key _arrays,sort_asc,sort_numeric, $data);
Rturn $data;
}
For more information about PHP interested readers can view the site topics: "PHP array Operation skills Encyclopedia", "PHP Sorting algorithm Summary", "PHP Basic Grammar Primer", "PHP Operations and Operator Usage Summary", "PHP object-oriented Programming Program Introduction", " PHP Network Programming skills Summary, "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"
I hope this article will help you with the PHP program design.