按子数组值对数组索引排序

编程入门 行业动态 更新时间:2024-10-18 03:31:01
本文介绍了按子数组值对数组索引排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我具有以下数组结构,但我需要按标记值排序,并且在重复时需要最后按数组索引排序:

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 ) )

更多推荐

按子数组值对数组索引排序

本文发布于:2023-11-30 17:34:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650912.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   索引

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!