所以有这样一个“美丽”的多维数组:
array
0 =>
array
0 =>
object(SimpleXMLElement)
public 'name' => string 'some name'
public 'model' => string 'some model'
1 =>
object(SimpleXMLElement)
public 'name' => string 'some name'
public 'model' => string 'some model'
1 =>
array
0 =>
object(SimpleXMLElement)
public 'name' => string 'some name'
public 'model' => string 'some model'
1 =>
object(SimpleXMLElement)
public 'name' => string 'some name'
public 'model' => string 'some model'
and so on
我删除了中间数组,得到一个带有循环的数组(并将对象转换为数组):
foreach ($items as $x) {
foreach ($x as $y) {
$item[] = (array) $y;
}
}
结果如下:
array
0 =>
array
'name' => string 'some name'
'model' => string 'some model'
1 =>
array
'name' => string 'some name'
'model' => string 'some model'
2 => ...
3 => ...
etc.
这段代码可以实现目标(创建一个包含4个数组的数组),但我想知道是否有更简洁的方法?循环遍历1000多个数组肯定不是最好的选择。我不需要具体的代码,只需要思路。
array_walk_recursive()
。但在后端,它是相同的算法。 - Sam Dufelarray_walk_recursive()
只访问叶节点。 - mickmackusa