我有一个由单词和数字混合而成的嵌套数组。它看起来像这样概念上。我只需要处理编号索引,例如 15
和 28
。我想这意味着我不能使用 foreach
循环(或者有什么方法可以)。你会怎么做?
myarray = (
someindex = (
field1 =
field2 =
);
15 = (
field1 =
field2 =
);
28 = (
field1 =
field2 =
);
anothertext = (
field1 =
field2 =
);
);
最佳答案
foreach($myarr as $key => $item)
{
if(is_int($key))
{
// Do processing here
}
}
是的,这将遍历数组中的每个项,因此如果您想单独处理其他项,您可以添加一个else
block 。
编辑:将 is_numeric
更改为 is_int
。请参阅评论以获取解释。
关于php - 在数组中选择自定义索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3578871/