简体   繁体   English

如何排序多维数组的第二个索引?

[英]How to sort second index of multidimensional array?

Not sure if titled the question correctly but first of all 不知道标题是否正确,但首先

What kind of array is this? 这是什么样的数组? Second how do I sort the second index by ASC or DESC 其次,如何按ASC或DESC对第二个索引进行排序

For example where it says float(.... How do I sort that? 例如,它说的是float(....如何排序?

Been trying to use rsort and arsort but just not getting the results I need. 一直在尝试使用rsort和arsort,但没有得到我需要的结果。

If I have code like this 如果我有这样的代码

array(24) { 
           [0]=> array(2) 
           { [0]=> string(4) "AAPL" [1]=> float(64.756994020789) }

           [1]=> array(2) 
           { [0]=> string(3) "AMD" [1]=> float(57.268267955388) }

           [2]=> array(2) 
           { [0]=> string(4) "BABA" [1]=> float(57.24625652504) } 

           [3]=> array(2) 
           { [0]=> string(4) "BIDU" [1]=> float(65.24289909913) }

           [4]=> array(2) 
           { [0]=> string(5) "BRK.B" [1]=> float(52.178207183616) }

           [5]=> array(2) 
           { [0]=> string(3) "CAT" [1]=> float(50.412401981782) }

           [6]=> array(2) 
           { [0]=> string(3) "DIA" [1]=> float(55.683323570131) }

           [7]=> array(2) 
           { [0]=> string(3) "DIS" [1]=> float(47.435360931467) }

           } 

           rsort($array);
           var_dump($array);

Use usort with your custom sorting function: usort与您的自定义排序功能一起使用:

usort($array, function($a, $b) { return $a[1] - $b[1]; });
// as of php7
usort($array, function($a, $b) { return $a[1] <=> $b[1]; });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM