列"在一个以上的QUOT MultiDiminsional数组排序数组; (钥匙)使用指定排序选项 [英] Sort Array of MultiDiminsional Arrays on More Than One "Column" (Key) With Specified Sort Options
问题描述
我期待能够在多列排序多维数组的数组。为了进一步复杂化,我想可以设置每个键/列具体排序选项。我有什么是类似于DB查询的结果,但实际上并没有来自一个,因此它在PHP,而非SQL排序的需要。
阵列
(
[0] =>排列
(
[FIRST_NAME] =>荷马
[姓氏] =>辛普森
[城市] =>斯普林菲尔德
[状态] =>未知
[邮编] => 66735
) [1] =>排列
(
[FIRST_NAME] =>帕蒂
[姓氏] =>布维尔
[城市] =>斯科茨代尔
[状态] =>亚利桑那
[邮编] => 85250
) [2] =>排列
(
[FIRST_NAME] =>萌
[姓氏] => Szyslak
[城市] =>斯科茨代尔
[状态] =>亚利桑那
[邮编] => 85255
) [3] =>排列
(
[FIRST_NAME] =>缺口
[姓氏] =>里维埃拉
[城市] =>斯科茨代尔
[状态] =>亚利桑那
[邮编] => 85255
))
我希望能对它进行排序相似,这可能与一个数据库查询来完成。哦,有时列/键需要用数字来指定。
我脑子里想的是类似于这样:
$ sortOptions =阵列(阵列(城市,SORT_ASC,SORT_STRING)
阵列('拉链',SORT_DESC,SORT_NUMERIC)
阵列(2 SORT_ASC,SORT_STRING)// 2 ='姓氏'
);
$分拣机=新MultiSort($数据,$ sortOptions);
$ sortedData = $ sorter-> getSortedArray();
的print_r($ jmsSorted);
我愿与落得是这样的:
阵列
(
[0] =>排列
(
[FIRST_NAME] =>缺口
[姓氏] =>里维埃拉
[城市] =>斯科茨代尔
[状态] =>亚利桑那
[邮编] => 85255
) [1] =>排列
(
[FIRST_NAME] =>萌
[姓氏] => Szyslak
[城市] =>斯科茨代尔
[状态] =>亚利桑那
[邮编] => 85255
) [2] =>排列
(
[FIRST_NAME] =>帕蒂
[姓氏] =>布维尔
[城市] =>斯科茨代尔
[状态] =>亚利桑那
[邮编] => 85250
) [3] =>排列
(
[FIRST_NAME] =>荷马
[姓氏] =>辛普森
[城市] =>斯普林菲尔德
[状态] =>未知
[邮编] => 66735
))
更新:我认为,理想情况下,解决方案将导致动态创建
在array_multisort($城市,SORT_ASC,SORT_STRING,$拉链,SORT_DESC,SORT_NUMERIC,$姓氏,SORT_ASC,SORT_STRING,$ inputArray);
问题是,我不希望有硬code中就有这些关键的名字。我试过3排序数据库结果从创建基于解决方案实例# 在array_multisort()
该结束了使用在array_multisort()文档
但我似乎无法找到一种方法,用我的动态生成的参数列表在array_multisort()
。
我的尝试是为链这些参数组合成一个数组,然后
call_user_func_array('在array_multisort',$ functionArgs);
这导致一个
警告:参数2在array_multisort()预计将在给出一个参考,值...
下面是我最后决定了能够多维数组排序。上述两个问题的答案是好的,但我也寻找一些灵活性。
我绝对不认为有任何一个正确的答案,但是这是适合我的需求和灵活。
你可以从我的 @link
在 _usortByMultipleKeys()的意见见
它是从评论改编在PHP手册中,目前似乎并不存在,但我相信 HTTP ://www.php.net/manual/en/function.usort.php#104398 是原来的注释的新版本。使用新的建议,我还没有探索。
/ **
*排序的结果集。
*
*用法:$ sortOptions =阵列(
*'节',//默认为SORT_ASC
*'行'=> SORT_DESC,
*'retail_price'=> SORT_ASC);
* $结果> sortResults($ sortOptions);
*
* @参数数组$ sortOptions排序指令数组
* /
公共职能sortResults(数组$ sortOptions)
{
usort($这个 - > _results,$这个 - > _usortByMultipleKeys($ sortOptions));
}
/ **
*由sortResults使用()
*
* @link http://www.php.net/manual/en/function.usort.php#103722
* /
保护功能_usortByMultipleKeys($键,$ =方向SORT_ASC)
{
$ sortFlags =阵列(SORT_ASC,SORT_DESC);
如果(!in_array($方向,$ sortFlags)){
抛出新InvalidArgumentException('排序标志只接受SORT_ASC或SORT_DESC');
}
返回功能($ A,$ B)使用($键,$方向,$ sortFlags){
如果(!is_array($键)){//只有一个键和排序方向
如果(使用isset($ A->!$键)||使用isset($ B-> $键)){
抛出新的异常(试图排序不存在的钥匙');
}
如果($ A-> $键== $ B-> $键){
返回0;
}
回报($方向== SORT_ASC XOR $ A-> $键< $ B-> $键)? 1:-1;
}其他{//使用排序和子排序多键
的foreach($键$子=> $ subAsc){
//阵列来作为'sort_key'=> SORT_ASC | SORT_DESC或只是sort_key,所以需要检测哪些
如果(!in_array($ subAsc,$ sortFlags)){
$子项= $ subAsc;
$ subAsc = $方向;
}
//就像上面,除了继续代替回报0
如果(使用isset($ A->!$子项)||使用isset($ B-> $子项)){
抛出新的异常(试图排序不存在的钥匙');
}
如果($ A-> $子项== $ B-> $子项){
继续;
}
回报($ subAsc == SORT_ASC XOR $ A-> $子项< $ B-> $子项)? 1:-1;
}
返回0;
}
};
}
I'm looking to be able to sort an array of multi-dimensional arrays on more than one column. To further complicate it I'd like to be able to set specific sort options per key/column. I have what is similar the result of a DB query, but doesn't actually come from one, therefore the need to sort it in PHP rather than SQL.
Array
(
[0] => Array
(
[first_name] => Homer
[last_name] => Simpson
[city] => Springfield
[state] => Unknown
[zip] => 66735
)
[1] => Array
(
[first_name] => Patty
[last_name] => Bouvier
[city] => Scottsdale
[state] => Arizona
[zip] => 85250
)
[2] => Array
(
[first_name] => Moe
[last_name] => Szyslak
[city] => Scottsdale
[state] => Arizona
[zip] => 85255
)
[3] => Array
(
[first_name] => Nick
[last_name] => Riviera
[city] => Scottsdale
[state] => Arizona
[zip] => 85255
)
)
I would like to be able to sort it similar to what could be done with a DB query. Oh, and sometimes a column/key needs to be specified by number.
What I had in mind was something similar to this:
$sortOptions = array( array( 'city', SORT_ASC, SORT_STRING ),
array( 'zip', SORT_DESC, SORT_NUMERIC),
array( 2, SORT_ASC, SORT_STRING) // 2='last_name'
);
$sorter = new MultiSort($data, $sortOptions );
$sortedData = $sorter->getSortedArray() ;
print_r( $jmsSorted);
What I would like to end up with is this:
Array
(
[0] => Array
(
[first_name] => Nick
[last_name] => Riviera
[city] => Scottsdale
[state] => Arizona
[zip] => 85255
)
[1] => Array
(
[first_name] => Moe
[last_name] => Szyslak
[city] => Scottsdale
[state] => Arizona
[zip] => 85255
)
[2] => Array
(
[first_name] => Patty
[last_name] => Bouvier
[city] => Scottsdale
[state] => Arizona
[zip] => 85250
)
[3] => Array
(
[first_name] => Homer
[last_name] => Simpson
[city] => Springfield
[state] => Unknown
[zip] => 66735
)
)
UPDATE: I think that ideally, a solution would result in dynamically creating
array_multisort( $city, SORT_ASC, SORT_STRING, $zip, SORT_DESC, SORT_NUMERIC, $last_name, SORT_ASC, SORT_STRING, $inputArray);
The problem is that I don't want to have to "hard code" those key names in there. I tried creating a solution based upon Example #3 Sorting database results from the array_multisort()
documentation that ended up using array_multisort()
but I cannot seem to find a way to use my dynamically built argument list for array_multisort()
.
My attempt was to "chain" those arguments together into an array and then
call_user_func_array( 'array_multisort', $functionArgs);
That results in an
Warning: Parameter 2 to array_multisort() expected to be a reference, value given in...
Here is what I finally settled on for being able to sort multi-dimensional arrays. Both of the answers above are good but I was also looking for something flexible.
I definitely don’t think there is any one "right" answer, but this is what works for my needs and is flexible.
As you can see from my @link
in the comment of _usortByMultipleKeys()
it was adapted from a comment in the PHP manual that currently doesn't seem to exist, but I believe http://www.php.net/manual/en/function.usort.php#104398 is a new version of the original comment. I have not explored using that new suggestion.
/**
* Sort the resultSet.
*
* Usage: $sortOptions = array(
* 'section', // Defaults to SORT_ASC
* 'row' => SORT_DESC,
* 'retail_price' => SORT_ASC);
* $results->sortResults($sortOptions);
*
* @param array $sortOptions An array of sorting instructions
*/
public function sortResults(array $sortOptions)
{
usort($this->_results, $this->_usortByMultipleKeys($sortOptions));
}
/**
* Used by sortResults()
*
* @link http://www.php.net/manual/en/function.usort.php#103722
*/
protected function _usortByMultipleKeys($key, $direction=SORT_ASC)
{
$sortFlags = array(SORT_ASC, SORT_DESC);
if (!in_array($direction, $sortFlags)) {
throw new InvalidArgumentException('Sort flag only accepts SORT_ASC or SORT_DESC');
}
return function($a, $b) use ($key, $direction, $sortFlags) {
if (!is_array($key)) { //just one key and sort direction
if (!isset($a->$key) || !isset($b->$key)) {
throw new Exception('Attempting to sort on non-existent keys');
}
if ($a->$key == $b->$key) {
return 0;
}
return ($direction==SORT_ASC xor $a->$key < $b->$key) ? 1 : -1;
} else { //using multiple keys for sort and sub-sort
foreach ($key as $subKey => $subAsc) {
//array can come as 'sort_key'=>SORT_ASC|SORT_DESC or just 'sort_key', so need to detect which
if (!in_array($subAsc, $sortFlags)) {
$subKey = $subAsc;
$subAsc = $direction;
}
//just like above, except 'continue' in place of return 0
if (!isset($a->$subKey) || !isset($b->$subKey)) {
throw new Exception('Attempting to sort on non-existent keys');
}
if ($a->$subKey == $b->$subKey) {
continue;
}
return ($subAsc==SORT_ASC xor $a->$subKey < $b->$subKey) ? 1 : -1;
}
return 0;
}
};
}
这篇关于列&QUOT;在一个以上的QUOT MultiDiminsional数组排序数组; (钥匙)使用指定排序选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!