0

I have 2 multidimensional arrays. I need to merge the content from the second array into first array.

First array consists of dates and/or some urls.

array:2 [
  0 => array:3 [
    0 => array:2 [
      "startDate" => "2022-01-01"
      "endDate" => "2022-01-31"
    ]
    1 => array:2 [
      "startDate" => "2022-02-01"
      "endDate" => "2022-02-28"
    ]
    2 => array:4 [
      "startDate" => ""
      "endDate" => ""
      "bannerUrl" => "https://commons.wikimedia.org/wiki/File:Red_rose_flower_detailed_imge.jpg"
      "imageTarget" => "image Target"
    ]
  ]
  1 => array:1 [
    0 => array:2 [
      "startDate" => "2022-01-01"
      "endDate" => "2022-01-15"
    ]
  ]
]

Second array consists of image objects.

array:2 [
  0 => array:2 [
    0 => array:1 [
      "image" => {"image A object...."}
    ]
    1 => array:1 [
      "image" => {"image B object...."}
    ]
  ]
  1 => array:1 [
    0 => array:1 [
      "image" => {"image C object...."}
    ]
  ]
]

I need output like this where image from second array is passed to first array.

array:2 [
      0 => array:3 [
        0 => array:2 [
          "startDate" => "2022-01-01"
          "endDate" => "2022-01-31"
          "image" => {"image object...."}
        ]
        1 => array:2 [
          "startDate" => "2022-02-01"
          "endDate" => "2022-02-28"
          "image" => {"image object...."}
        ]
        2 => array:4 [
          "startDate" => ""
          "endDate" => ""
          "bannerUrl" => "https://commons.wikimedia.org/wiki/File:Red_rose_flower_detailed_imge.jpg"
          "imageTarget" => "image Target"
        ]
      ]
      1 => array:1 [
        0 => array:2 [
          "startDate" => "2022-01-01"
          "endDate" => "2022-01-15"
          "image" => {"image object...."}
        ]
      ]
    ]

This is code, I have tried.

foreach ($items as $key => $item) {
    for ($i = 0; $i < count($item); ++$i) {
        $executionArray[$key][$i] = $item[$i];
        if (isset($files[$key])) {
            if (isset($executionArray[$key][$i])) {
                $executionArray[$key][$i]['image'] = $files[$key][$i]['image'];
            }
        }
    }
}

But I am only getting Undefined array key 2.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
user1687891
  • 794
  • 2
  • 14
  • 30
  • Please always present your array/object data as the output from `var_export()` so that contributors can instantly use it. – mickmackusa Aug 17 '22 at 22:37

2 Answers2

0
  • Iterate only on the second array.
  • Keep track of the first and second level keys which relate the data between the two arrays.
  • Push the new associative element (image object) into the first array.

Code: (Demo)

foreach ($array2 as $i1 => $rows) {
    foreach ($rows as $i2 => $row) {
        $array1[$i1][$i2] += $row;
    }
}
var_export($array1);

Output:

array (
  0 => 
  array (
    0 => 
    array (
      'startDate' => '2022-01-01',
      'endDate' => '2022-01-31',
      'image' => 
      (object) array(
         '0' => 'image A object....',
      ),
    ),
    1 => 
    array (
      'startDate' => '2022-02-01',
      'endDate' => '2022-02-28',
      'image' => 
      (object) array(
         '0' => 'image B object....',
      ),
    ),
    2 => 
    array (
      'startDate' => '',
      'endDate' => '',
      'bannerUrl' => 'https://commons.wikimedia.org/wiki/File:Red_rose_flower_detailed_imge.jpg',
      'imageTarget' => 'image Target',
    ),
  ),
  1 => 
  array (
    0 => 
    array (
      'startDate' => '2022-01-01',
      'endDate' => '2022-01-15',
      'image' => 
      (object) array(
         '0' => 'image C object....',
      ),
    ),
  ),
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
-1

Maybe you can use array_merge inside the foreach loop.

$out = array();
foreach ($arr1 as $key => $value){
    $out[] = (object)array_merge((array)$arr2[$key], (array)$value);
}
print_r($out)

Source.

  • Welcome to SO! Please don't post link-only answers but summarize the linked content or its relevant parts a little. A link may become invalid or outdated and in that case, your answer would be useless in the future. – ahuemmer Aug 16 '22 at 08:31
  • If another page provides the required resolving advice, please flag to close as a duplicate instead of answering. – mickmackusa Aug 17 '22 at 22:37
  • This answer is [provably incorrect](https://3v4l.org/bH0EX). Please test your code before posting an answer. – mickmackusa Aug 17 '22 at 23:35