-3

I want to change the key of my array in php.

Here an exemple :

array (size=5)
  0 => 
    array (size=2)
      'iden' => string '01' (length=8)
      'don' => string '17' (length=2)
  1 => 
    array (size=2)
      'iden' => string '02' (length=8)
      'don' => string '17' (length=2)
  2 => 
    array (size=2)
      'iden' => string '03' (length=8)
      'don' => string '17' (length=2)

And I want to change my array like this :

array (size=5)
  0 => 
    array (size=2)
      0 => string '01' (length=8)
      1 => string '17' (length=2)
  1 => 
    array (size=2)
      0 => string '02' (length=8)
      1 => string '17' (length=2)
  2 => 
    array (size=2)
      0 => string '03' (length=8)
      1 => string '17' (length=2)

Thanks in advance

0

1 Answer 1

2

You can use the array_values function to remove named keys:

foreach($array as &$item) {
    $item = array_values($item);
}
unset($item); // Remove reference

Note the & in the foreach. This creates a reference in the $item variable to the corresponding array element which means you can edit it in your loop.

If you want, you can also write this in a single line using array_map:

$array = array_map("array_values", $array);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.