2017-10-04 72 views
1

我在我的網站上使用自定義類模板。我剛剛遇到了一種情況,我需要將另一個模板與另一個模板同步追加。如何結合兩個具有相同索引的多維數組而不合並?

模板對象數組在結構和數據上非常相似,但是數組#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

你可以像下面: -

$final_array = []; 

foreach($array1 as $key=>$arr){ 
$final_array[$key] = [$arr,$array2[$key]]; 
} 
print_r($final_array); 
相關問題