PHP获取数组中的所有数字/匿名键

时间:2022-09-08 21:37:45

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.

有任何想法吗?谢谢你的帮助。

3 个解决方案

#1


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
}

#2


1  

$result = array();
foreach ($initial_array as $key => $value)
  if ( ! is_string( $key ) )
    $result[ $key ] = $value;

#3


0  

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

键是0,不应该是$ your_array [0]?

#1


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
}

#2


1  

$result = array();
foreach ($initial_array as $key => $value)
  if ( ! is_string( $key ) )
    $result[ $key ] = $value;

#3


0  

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

键是0,不应该是$ your_array [0]?