PHP Get all numeric / anonymous keys in an array

I have a multidimensional array, I'm interested in getting all the elements (at the same level) that do not have named keys.

i.e.

Array
{
  ['settings'] {...}
  ['something'] {...}
  [0] {...} // I want this one
  ['something_else'] {...}
  [1] {...} // And this one
}

Any ideas? Thank you for your help.

+5
source share
3 answers

This is one way.

foreach (array_keys($array) as $key) {
 if(is_int($key)) {
  //do something
 }
}

EDIT

Depending on the size of your array, there may be faster and more memory instead. However, it requires the keys to be in order, and none of them are missing.

for($i=0;isset($array[$i]);$i++){
 //do something
}
+6
source
$result = array();
foreach ($initial_array as $key => $value)
  if ( ! is_string( $key ) )
    $result[ $key ] = $value;
+1
source

The key 0should not be $your_array[0]?

0
source

All Articles