我在我的网站上使用自定义类进行模板化。我刚遇到一种情况,我需要同步附加一个模板。
模板对象数组在结构和数据上非常相似,但是,数组#2包含几个子数组,它们是数组#1的子元素。
数组#1
0 => object(Template)[59] protected 'file' => string 'template/completed.tpl' protected 'values' => array (size=3) 'part' => string 'door' 'part_status' => string 'warehouse' 'part_type' => string 'consumable' 1 => object(Template)[61] protected 'file' => string 'template/completed.tpl' protected 'values' => array (size=4) 'partNum' => string '3741852' 'part' => string 'bolt' 'part_status' => string 'shipped' 'part_type' => string 'consumable' 2 => object(Template)[63] protected 'file' => string 'template/completed.tpl' protected 'values' => array (size=4) 'partNum' => string '3741777' 'part' => string 'frame' 'part_status' => string 'shipped' 'part_type' => string 'consumable' 10 => (RELATED TO INDEX 10 IN ARRAY #2 HOWEVER NO SUBARRAYS) object(Template)[65] protected 'file' => string 'template/completed.tpl' protected 'values' => array (size=4) 'partNum' => string '3849999' 'part' => string 'cable' 'part_status' => string 'backorder' 'part_type' => string 'consumable' ...
数组#2
0 =>
object(Template)[33]
protected 'file' => string 'template/details.tpl'
protected 'values' =>
array (size=3)
'part' => string 'door'
'part_status' => string 'warehouse'
'part_type' => string 'consumable'
1 =>
object(Template)[35]
protected 'file' => string 'template/details.tpl'
protected 'values' =>
array (size=4)
'partNum' => string '3741852'
'part' => string 'bolt'
'part_status' => string 'shipped'
'part_type' => string 'consumable'
2 =>
object(Template)[37]
protected 'file' => string 'template/details.tpl'
protected 'values' =>
array (size=4)
'partNum' => string '3741852'
'part' => string 'bolt'
'part_status' => string 'shipped'
'part_type' => string 'consumable'
...
10 =>
array (size=2)
0 =>
object(Template)[44]
protected 'file' => string 'template/details.tpl'
protected 'values' =>
array (size=4)
'partNum' => string '3741852'
'part' => string 'bolt'
'part_status' => string 'shipped'
'part_type' => string 'consumable'
1 =>
...
我的想法是将两个数组合并然后循环通过它们但我不知道该怎么做。我首先考虑array_merge
,但它将数据附加在一起并保持相同的索引,这不是我需要的。我希望找到一种方法来保持相同的父索引,然后添加每个索引自己的子索引。还有阵列#2具有子阵列的问题。我还想过尝试同时遍历两个数组,但这似乎是一种混乱的方式。
0 =>
array (size=2)
0 =>
object(Template)[59]
protected 'file' => string 'template/completed.tpl'
protected 'values' =>
array (size=3)
'part' => string 'door'
'part_status' => string 'warehouse'
'part_type' => string 'consumable'
1 =>
object(Template)[33]
protected 'file' => string 'template/details.tpl'
protected 'values' =>
array (size=4)
'partNum' => string '3741852'
'part' => string 'door'
'part_status' => string 'warehouse'
'part_type' => string 'consumable'
1 =>
array (size=2)
0 =>
object(Template)[61]
protected 'file' => string 'template/completed.tpl'
protected 'values' =>
array (size=4)
'partNum' => string '3741852'
'part' => string 'handle'
'part_status' => string 'backorder'
'part_type' => string 'consumable'
...
答案 0 :(得分:0)
你可以这样做: -
$final_array = [];
foreach($array1 as $key=>$arr){
$final_array[$key] = [$arr,$array2[$key]];
}
print_r($final_array);