我具有以下数组结构,但我需要按标记值排序,并且在重复时需要最后按数组索引排序:
I've got the following structue of array, but i need sort by mark value and on duplicated need sort at last by array index:
这是我的数组基本 $ 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 } } }什么是最好的方法按子数组对数组进行排序,并重复使用索引数组对数组进行排序?
What's the best way for order the array by sub-array and on duplicated use the Index array, incremental?
所以结果看起来像这样:
So the results look like this:
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中它没有所需的作用域:
I try to use this, but it does not have the desired scope with php 7.2:
usort($AsocContData[$s_list_100], function ($a, $b) { return $a['Prod_Mark'] <=> $b['Prod_Mark']; });此返回值而不是重复项:
this return and not sort for duplicates:
array(20) { [0]=>// remplace this first Index array with position and it is a fail. array(38) { } }更新
基本数组是动态嵌套的,并且可以嵌套更多的Array子级。
the base array is dynamic nested, and can have much more Array child nested.
推荐答案您可能应该使用 array_multisort()
You should probably use 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 ) )更多推荐
按子数组值对数组索引排序
发布评论