PHP 按子数组值对数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2477496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Sort Array By SubArray Value
提问by Sjwdavies
I have the following array structure:
我有以下数组结构:
Array
(
[0] => Array
(
[configuration_id] => 10
[id] => 1
[optionNumber] => 3
[optionActive] => 1
[lastUpdated] => 2010-03-17 15:44:12
)
[1] => Array
(
[configuration_id] => 9
[id] => 1
[optionNumber] => 2
[optionActive] => 1
[lastUpdated] => 2010-03-17 15:44:12
)
[2] => Array
(
[configuration_id] => 8
[id] => 1
[optionNumber] => 1
[optionActive] => 1
[lastUpdated] => 2010-03-17 15:44:12
)
)
What is the best way to order the array in an incremental way, based on the optionNumber?
基于optionNumber?以增量方式对数组进行排序的最佳方法是什么?
So the results look like:
结果如下:
Array
(
[0] => Array
(
[configuration_id] => 8
[id] => 1
[optionNumber] => 1
[optionActive] => 1
[lastUpdated] => 2010-03-17 15:44:12
)
[1] => Array
(
[configuration_id] => 9
[id] => 1
[optionNumber] => 2
[optionActive] => 1
[lastUpdated] => 2010-03-17 15:44:12
)
[2] => Array
(
[configuration_id] => 10
[id] => 1
[optionNumber] => 3
[optionActive] => 1
[lastUpdated] => 2010-03-17 15:44:12
)
)
回答by kennytm
Use usort.
使用usort.
function cmp_by_optionNumber($a, $b) {
return $a["optionNumber"] - $b["optionNumber"];
}
...
usort($array, "cmp_by_optionNumber");
In PHP ≥5.3, you should use an anonymous functioninstead:
在 PHP ≥5.3 中,您应该使用匿名函数代替:
usort($array, function ($a, $b) {
return $a['optionNumber'] - $b['optionNumber'];
});
Note that both code above assume $a['optionNumber']is an integer. Use @St. John Johnson's solutionif they are strings.
请注意,上面的两个代码都假设$a['optionNumber']是一个整数。使用@St。如果它们是字符串,则约翰约翰逊的解决方案。
In PHP ≥7.0, use the spaceship operator <=>instead of subtraction to prevent overflow/truncation problems.
在 PHP ≥7.0 中,使用spaceship 运算符<=>而不是减法来防止溢出/截断问题。
usort($array, function ($a, $b) {
return $a['optionNumber'] <=> $b['optionNumber'];
});
回答by St. John Johnson
Use usort
用 usort
usort($array, 'sortByOption');
function sortByOption($a, $b) {
return strcmp($a['optionNumber'], $b['optionNumber']);
}
回答by Pigalev Pavel
I used both solutions by KennyTMand AJ Quickand came up with a function that can help in this issue for many cases like using ASC or DESCsorting or preserving keysor if you have objects as children of array.
我使用了KennyTM和AJ Quick 的两种解决方案,并提出了一个函数,可以在许多情况下帮助解决这个问题,例如使用 ASC 或 DESC排序或保留键,或者如果您有对象作为数组的子项。
Here is this function (works for PHP7 and higher because of spaceship operator):
这是这个函数(适用于 PHP7 及更高版本,因为飞船操作员):
/**
* @param array $array
* @param string $value
* @param bool $asc - ASC (true) or DESC (false) sorting
* @param bool $preserveKeys
* @return array
* */
function sortBySubValue($array, $value, $asc = true, $preserveKeys = false)
{
if ($preserveKeys) {
$c = [];
if (is_object(reset($array))) {
foreach ($array as $k => $v) {
$b[$k] = strtolower($v->$value);
}
} else {
foreach ($array as $k => $v) {
$b[$k] = strtolower($v[$value]);
}
}
$asc ? asort($b) : arsort($b);
foreach ($b as $k => $v) {
$c[$k] = $array[$k];
}
$array = $c;
} else {
if (is_object(reset($array))) {
usort($array, function ($a, $b) use ($value, $asc) {
return $a->{$value} == $b->{$value} ? 0 : ($a->{$value} <=> $b->{$value}) * ($asc ? 1 : -1);
});
} else {
usort($array, function ($a, $b) use ($value, $asc) {
return $a[$value] == $b[$value] ? 0 : ($a[$value] <=> $b[$value]) * ($asc ? 1 : -1);
});
}
}
return $array;
}
Usage:
用法:
sortBySubValue($array, 'optionNumber', true, false);
Edit
编辑
The first part can be rewritten using uasort()and the function will be shorter (works for PHP7 and higher because of spaceship operator):
第一部分可以使用重写uasort(),函数会更短(适用于 PHP7 及更高版本,因为太空船运算符):
/**
* @param array $array
* @param string $value
* @param bool $asc - ASC (true) or DESC (false) sorting
* @param bool $preserveKeys
* @return array
* */
function sortBySubValue($array, $value, $asc = true, $preserveKeys = false)
{
if (is_object(reset($array))) {
$preserveKeys ? uasort($array, function ($a, $b) use ($value, $asc) {
return $a->{$value} == $b->{$value} ? 0 : ($a->{$value} <=> $b->{$value}) * ($asc ? 1 : -1);
}) : usort($array, function ($a, $b) use ($value, $asc) {
return $a->{$value} == $b->{$value} ? 0 : ($a->{$value} <=> $b->{$value}) * ($asc ? 1 : -1);
});
} else {
$preserveKeys ? uasort($array, function ($a, $b) use ($value, $asc) {
return $a[$value] == $b[$value] ? 0 : ($a[$value] <=> $b[$value]) * ($asc ? 1 : -1);
}) : usort($array, function ($a, $b) use ($value, $asc) {
return $a[$value] == $b[$value] ? 0 : ($a[$value] <=> $b[$value]) * ($asc ? 1 : -1);
});
}
return $array;
}
回答by Ghanshyam Nakiya
回答by AJ Quick
The keys are removed when using a function like the ones above. If the keys are important, the following function would maintain it... but foreach loops are pretty inefficient.
使用上述功能时,键将被删除。如果键很重要,下面的函数会维护它……但是 foreach 循环效率很低。
function subval_sort($a,$subkey) {
foreach($a as $k=>$v) {
$b[$k] = strtolower($v[$subkey]);
}
asort($b);
foreach($b as $key=>$val) {
$c[$key] = $a[$key];
}
return $c;
}
$array = subval_sort($array,'optionNumber');
Use arsort instead of asort if you want from high to low.
如果要从高到低,请使用arsort 而不是asort。
Code credit: http://www.firsttube.com/read/sorting-a-multi-dimensional-array-with-php/
代码信用:http: //www.firsttube.com/read/sorting-a-multi-dimensional-array-with-php/
回答by Samer Ata
PHP 5.3+
PHP 5.3+
usort($array, function($a,$b){ return $a['optionNumber']-$b['optionNumber'];} );

