简体   繁体   中英

How to combine two multidimensional arrays with same index without merging?

I use a custom class for templating on my site. I just came across a situation where I need to append one template with another synchronously.

The template object arrays are very similar in structure and data, however, array #2 contains several subarrays which are children of array #1.

Array #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 => 
         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'
    ...

Array #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 =>
    ...

My thought is to combine the two arrays then loop through them together but I don't know how to do that. I first thought about array_merge , but it appends the data together and keeps the same index which is not what I need. I'm hoping to find a way to keep the same parent index then add each with it's own child index. There's also the issue that array #2 has subarrays. I also thought about trying to loop through both arrays simultaneously but that seems like a messy way to do it.

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'
...

You can do it like below:-

$final_array = [];

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM