5

I have a multidimensional array, I am interested in getting all the elements (one level deep) that don't have named keys.

i.e.

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

Any ideas? Thanks for your help.

Dominic
  • 62,658
  • 20
  • 139
  • 163

3 Answers3

6

This is one way

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

EDIT

Depending on the size of your array it may be faster and more memory efficient to do this instead. It does however require that the keys are in order and none are missing.

for($i=0;isset($array[$i]);$i++){
 //do something
}
ian.shaun.thomas
  • 3,468
  • 25
  • 40
1
$result = array();
foreach ($initial_array as $key => $value)
  if ( ! is_string( $key ) )
    $result[ $key ] = $value;
linepogl
  • 9,147
  • 4
  • 34
  • 45
0

The key is 0, Shouldn't be $your_array[0]?

xdazz
  • 158,678
  • 38
  • 247
  • 274