我具有以下数组结构,但我需要按标记值排序,并且在重复时需要最后按数组索引排序:
这是我的数组基础$AsocContData[$s_list_100]
:
array(5) {
[01081] => Array(3){
[id] => 2
[Mark] => 420
[lastUpdated] => 2010-03-17 15:44:12
[dataNext] => Array(2){
[more1] => 54
[More2] => 54
}
}
[00358] => Array(3){
[id] => 6
[Mark] => 500
[lastUpdated] => 2010-03-17 15:44:12
[dataNext] => Array(2){
[more1] => 54
[More2] => 54
}
}
[00277] => Array(3){
[id] => 3
[Mark] => 400
[lastUpdated] => 2010-03-17 15:44:12
[dataNext] => Array(2){
[more1] => 54
[More2] => 54
}
}
[00357] => Array(3){
[id] => 1
[Mark] => 500
[lastUpdated] => 2010-03-17 15:44:12
[dataNext] => Array(2){
[more1] => 54
[More2] => 54
}
}
}
按子数组排序数组并重复使用索引数组的最佳方法是什么?
结果如下:
array(5) {
[00277] => Array(3){
[id] => 3
[Mark] => 400
[lastUpdated] => 2010-03-17 15:44:12
[dataNext] => Array(2){
[more1] => 54
[More2] => 54
}
}
[01081] => Array(3){
[id] => 2
[Mark] => 420
[lastUpdated] => 2010-03-17 15:44:12
[dataNext] => Array(2){
[more1] => 54
[More2] => 54
}
}
[00357] => Array(3){
[id] => 1
[Mark] => 500
[lastUpdated] => 2010-03-17 15:44:12
[dataNext] => Array(2){
[more1] => 54
[More2] => 54
}
}
[00358] => Array(3){
[id] => 6
[Mark] => 500
[lastUpdated] => 2010-03-17 15:44:12
[dataNext] => Array(2){
[more1] => 54
[More2] => 54
}
}
}
更新
我尝试使用它,但是在php 7.2中它没有所需的作用域:
usort($AsocContData[$s_list_100], function ($a, $b) {
return $a['Prod_Mark'] <=> $b['Prod_Mark'];
});
此返回,不对重复项进行排序:
array(20) {
[0]=>// remplace this first Index array with position and it is a fail.
array(38) {
}
}
更新
基本数组是动态嵌套的,可以嵌套更多的Array子级。
答案 0 :(得分:1)
<?php
$list = [
277 => [
'id' => 3,
'Mark' => 400,
'lastUpdated' => '2010-03-17 15:44:12'
],
1081 => [
'id' => 2,
'Mark' => 420,
'lastUpdated' => '2010-03-17 15:44:12'
],
357 => [
'id' => 1,
'Mark' => 500,
'lastUpdated' => '2010-03-17 15:44:12'
],
358 => [
'id' => 6,
'Mark' => 500,
'lastUpdated' => '2010-03-17 15:44:12'
]
];
$tmp = array_column($list, 'Mark');
array_multisort($tmp, SORT_ASC, array_keys($list), SORT_ASC, $list);
var_dump($list);
应该这样做
答案 1 :(得分:1)
您可能应该使用array_multisort()
array_multisort(
array_column( $AsocContData[$s_list_100], 'Mark' ), // sort by the Mark sub-array value first
SORT_ASC, // ascending
SORT_NUMERIC, // treat data as numeric
array_keys( $AsocContData[$s_list_100] ), // secondly, sort by the array indexes
SORT_ASC, // ascending
SORT_NUMERIC, // treat indexes as numeric
$AsocContData[$s_list_100] // this array will be sorted in place and by reference
);
var_dump( $AsocContData[$s_list_100] );
输入:
$AsocContData[$s_list_100] = array(
'01081' => Array(
'id' => 2,
'Mark' => 420,
'lastUpdated' => '2010-03-17 15:44:12'
),
'00358' => Array(
'id' => 6,
'Mark' => 500,
'lastUpdated' => '2010-03-17 15:44:12'
),
'00277' => Array(
'id' => 3,
'Mark' => 400,
'lastUpdated' => '2010-03-17 15:44:12'
),
'00357' => Array(
'id' => 1,
'Mark' => 500,
'lastUpdated' => '2010-03-17 15:44:12'
)
);
输出:
Array
(
[00277] => Array
(
[id] => 3
[Mark] => 400
[lastUpdated] => 2010-03-17 15:44:12
)
[01081] => Array
(
[id] => 2
[Mark] => 420
[lastUpdated] => 2010-03-17 15:44:12
)
[00357] => Array
(
[id] => 1
[Mark] => 500
[lastUpdated] => 2010-03-17 15:44:12
)
[00358] => Array
(
[id] => 6
[Mark] => 500
[lastUpdated] => 2010-03-17 15:44:12
)
)