'Get keys from associative array [duplicate]

I have this array:

Array ( [7] => 12 [5] => 11 [4] => 11 [2] => 9 [8] => 7 [1] => 6 [3] => 6 [6] => 5 [23] => 3 [31] => 2 [32] => 0 [30] => 0 [29] => 0 [21] => 0 [24] => 0 [22] => 0 )

I need to flip the array and then reset the keys so it starts like [0] => 7 [1] => 5...

So I did this:

//Flips the array
$arrayEquipas = array_flip($arrayOrdem);
//Resets the keys
$arrayEquipas = array_values($arrayEquipas);

The problem is, when I flip the array, it merges duplicate keys (which were previously values) and I don't want that to happen. Any ideas on how to do this?



Solution 1:[1]

The function that will do what you is array_keys.
It will extract all the keys, and map them to a new numeric array.

Start:

Array ( [7] => 12 [5] => 11 [4] => 11 [2] => 9 [8] => 7 [1] => 6 [3] => 6 [6] => 5 [23] => 3 [31] => 2 [32] => 0 [30] => 0 [29] => 0 [21] => 0 [24] => 0 [22] => 0 )

Code:

$arrayEquipas = array_keys($arrayOrdem);

Output:

array (size=16)
  0 => int 7
  1 => int 5
  2 => int 4
  3 => int 2
  4 => int 8
  5 => int 1
  6 => int 3
  7 => int 6
  8 => int 23
  9 => int 31
  10 => int 32
  11 => int 30
  12 => int 29
  13 => int 21
  14 => int 24
  15 => int 22

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Blaatpraat