0

我有一个这样的数组:

        Array
        (
           [1] => 0
           [2] => 1
           [4] => 1
           [5] => 4
           [6] => 2
           [7] => 0
           [8] => 2
           [9] => 7
           [10] => 2
           [11] => 2
           [12] => 2
           [17] => 12
           [18] => 17
           [19] => 0
           [20] => 19
           [21] => 20
           [22] => 20
           [23] => 20
           [24] => 0
         )

我想对数组进行排序,首先找到所有 0 值键索引,然后使用这个键值索引在数组中查找,比如 0 值键索引是 1,7,19,24,现在找到 1,7,19,24 值数组和对应的键被获取,最后所有的键序列都被获取到新的数组中。可能吗?

4

3 回答 3

3
asort($cars);

example:
asort($cars);
echo "<pre>";
print_r($cars);
echo "</pre>";
于 2013-07-30T06:34:19.870 回答
0
$newTab = array();

foreach ($tab as $k => $v)
{
    $newTab[$v][] = $k;
}

产生:

array (size=9)
  0 => 
    array (size=4)
      0 => int 1
      1 => int 7
      2 => int 19
      3 => int 24
  1 => 
    array (size=2)
      0 => int 2
      1 => int 4
  4 => 
    array (size=1)
      0 => int 5
  2 => 
    array (size=5)
      0 => int 6
      1 => int 8
      2 => int 10
      3 => int 11
      4 => int 12
   ....
于 2013-08-25T14:41:15.860 回答
0
asort($arr);
$arr = array_keys($arr);
print_r($arr);

输出:

Array
(
    [0] => 7
    [1] => 1
    [2] => 19
    [3] => 24
    [4] => 2
    [5] => 4
    [6] => 10
    [7] => 12
    [8] => 11
    [9] => 8
    [10] => 6
    [11] => 5
    [12] => 9
    [13] => 17
    [14] => 18
    [15] => 20
    [16] => 21
    [17] => 23
    [18] => 22
)
于 2013-07-30T06:39:11.720 回答