[英]Sort the array in descending order
I need to sort the array in descending order. 我需要按降序对数组进行排序。 I use
asort
in order to save proper reference to keys. 我使用
asort
来保存对键的正确引用。 However, $ind
is null. 但是,
$ind
为空。 Why? 为什么?
$selected = array();
for ($i=0; $i<10; $i++) {
$selected[] = array('ind' => $i, 'rank' => rand(0,10));
}
asort($selected, SORT_NUMERIC);
$ind = $selected['ind'];
After your for()
loop you have something like this: 在
for()
循环之后,您将得到以下内容:
Array (
[0] => Array (
[ind] => NUM
[rank] => NUM
[1] => Array (
[ind] => NUM
[rank] => NUM
etcetcetc....
)
This is called a multidimensional array, and you access the inner arrays in a similar way as you do single-dimensional arrays. 这称为多维数组,您可以像处理一维数组一样访问内部数组。
You can access it with a $array[0]['ind']
, or possibly even a foreach()
loop so you get all the values. 您可以使用
$array[0]['ind']
甚至可能使用foreach()
循环来访问它,以便获取所有值。
$ind = array();
foreach($array as $line) {
$ind[] = $line['ind'];
}
Now the $ind
array has all the values in a single-dimensional array, that you can access with: $ind[0]
or $ind[1]
, giving you the ind
value. 现在,
$ind
数组具有一维数组中的所有值,您可以使用: $ind[0]
或$ind[1]
获得ind
值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.