0

I have one array like this:

        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
         )

And i want to sort array like first all the 0 value key index find and and using this key value index find in array like 0 value key index is 1,7,19,24 and now 1,7,19,24 value find in array and corresponding key is get and at last all the key sequence is get in new array. is it possible?

darshan
  • 247
  • 1
  • 3
  • 12
  • What have you tried? Also, your question does not seem clear to me. What's your expected result array? – Carlos Campderrós Jul 30 '13 at 06:24
  • What is your expected array(result) look like ? – Prasanth Bendra Jul 30 '13 at 06:30
  • 0 is my root element then corresponding key index find which is 1,7,19,24 then 1,7,19,24 value find in array so for 1 we get 2,4 and now this 2,4 is further check so for 2 we get 6,8,10,11,12 and this value further check so for 6,8,10,11 we get nothing so skip and for 12 we get 17 so this type i want to check value – darshan Jul 30 '13 at 06:39
  • my array is look like Array ( [0] => 1 [1] => 2 [2] => 6 [3] => 8 [4] => 10 [5] => 12 [6] => 17 and so on – darshan Jul 30 '13 at 06:41

3 Answers3

2
asort($cars);

example:
asort($cars);
echo "<pre>";
print_r($cars);
echo "</pre>";
0
asort($arr);
$arr = array_keys($arr);
print_r($arr);

Output:

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
)
Expedito
  • 7,771
  • 5
  • 30
  • 43
0
$newTab = array();

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

Produces:

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
   ....
Michael D.
  • 264
  • 2
  • 5