Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Get all numerical/anonymous keys in an array

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.

like image 358
Dominic Avatar asked Jun 20 '12 15:06

Dominic


2 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 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
}
like image 150
ian.shaun.thomas Avatar answered Nov 14 '22 23:11

ian.shaun.thomas


$result = array();
foreach ($initial_array as $key => $value)
  if ( ! is_string( $key ) )
    $result[ $key ] = $value;
like image 37
linepogl Avatar answered Nov 14 '22 21:11

linepogl