Copy Code code as follows:
function Array_sort ($array, $key) {
if (Is_array ($array)) {
$key _array = null;
$new _array = null;
for ($i = 0; $i < count ($array); $i + +) {
$key _array[$array [$i] [$key]] = $i;
}
Ksort ($key _array);
$j = 0;
foreach ($key _array as $k => $v) {
$new _array[$j] = $array [$v];
$j + +;
}
unset ($key _array);
return $new _array;
}else{
return $array;
}
}
PHP two-dimensional array key value ordering
PHP Array_multisort () can be used to sort multiple arrays at once, or to sort multidimensional arrays based on one dimension or multidimensional. The association key name remains unchanged, but the numeric key name is indexed. The input array is treated as a table column and sorted by row, and the first array is the primary array to sort. The rows (values) in the array are the same, sorted by the size of the corresponding values in the next input array, and so on.
But if the array you want to sort is a two-dimensional array that needs to be sorted by the key values of the array, such as the following two-dimensional array, which needs to be sorted by the sort key name, then Array_multisort () cannot be directly implemented:
$data [5] = Array (' volume ' =>, ' Edition ' => 2);
$data [4] = Array (' volume ' =>, ' edition ' => 1);
$data [2] = Array (' volume ' =>, ' Edition ' => 6);
$data [3] = Array (' volume ' =>, ' Edition ' => 2);
$data [1] = Array (' volume ' =>, ' Edition ' => 6);
$data [6] = Array (' volume ' =>, ' edition ' => 7);
Prepares the array to sort by
foreach ($data as $k => $v) {
$edition [] = $v [' edition '];
}
Array_multisort ($edition, SORT_ASC, $data);
Print_r ($data);
Will output:
Array
(
[0] => array
(
[volume] =>
[Edition] => 1
)
[1] => array
(
[Volume] =>
[Edition] => 2
)
[2] => Array
(
[volume] =>
[Edition] => 2
)
[3] => array
(
[volume] =>
[Edition] => 6
)
[4] => array
( C24/>[volume] =>
[Edition] => 6
[5] => Array
(
[volume] =>
[Edition] => 7
)
)
To not break the original key, a sort function is written to support only two-dimensional arrays.
/** * sorted according to the size of a key value in the array, only two-dimensional array * * @param array $array ordered array * @param string $key key value * @p Aram BOOL $ASC Default positive sequence * @return array sort after arrays/function Arraysortbykey (array $array, $key, $asc = True) {$result = array
();
Sort out the array to be sorted foreach ($array as $k => & $v) {$values [$k] = Isset ($v [$key])? $v [$key]: ";
} unset ($v); Do you want to sort $asc for sort key values?
Asort ($values): Arsort ($values);
Rearrange the original array foreach ($values as $k => $v) {$result [$k] = $array [$k];
return $result; }