0

I need help to combine multiple arrays. For example, I have 2 different arrays.

$array1 contains the id and amount. $array2 will have the date and the amount base on the id.

$array1 = [
  310 => 0,
  311 => 0,
  312 => 0,
  313 => 0,
  314 => 0,
  315 => 0
];

$array2 = [
  "2019-05-17" => [
    312 => 3000.00
  ],
  "2019-06-20" => [
    312 => 3000.00
  ],
  "2019-06-27" => [
    313 => 5000.00,
    315 => 3000.00
  ]
];

Final output that I want:

$merge = [
    "2019-05-17" => [
      310 => 0,
      311 => 0,
      312 => 3000.00,
      313 => 0,
      314 => 0,
      315 => 0
    ],
    "2019-06-20" => [         
      310 => 0,
      311 => 0,
      312 => 3000.00,
      313 => 0,
      314 => 0,
      315 => 0
    ],
    "2019-06-27" => [
      310 => 0,       
      311 => 0,
      312 => 0,
      313 => 5000.00,
      314 => 0,
      315 => 3000.0
    ],
];

May I know how to achieve it so that I will get like the $merge?

2
  • We expect to see a failed coding attempt in your question. Commented Nov 3, 2019 at 9:54
  • 1
    Hi. We need to see what you tried. Thanks. Commented Nov 3, 2019 at 10:06

3 Answers 3

1

Do not iterate twice. Merge as you iterate in a single pass through the second array.

Merging the two arrays with the"union operator" (+) will result in unordered keyes (not that that is typically an issue with associative arrays). To use the default array as the "guide" for the subarrays' keys, call array_replace() as the merging function.

Code: (Demo)

foreach ($array2 as &$row) {
    $row += $array1;
    ksort($row);  // if you care
}

Or (Demo)

foreach ($array2 as &$row) {
    $row = array_replace($array1, $row);
}

Output:

array (
  '2019-05-17' => 
  array (
    310 => 0,
    311 => 0,
    312 => 3000.0,
    313 => 0,
    314 => 0,
    315 => 0,
  ),
  '2019-06-20' => 
  array (
    310 => 0,
    311 => 0,
    312 => 3000.0,
    313 => 0,
    314 => 0,
    315 => 0,
  ),
  '2019-06-27' => 
  array (
    310 => 0,
    311 => 0,
    312 => 0,
    313 => 5000.0,
    314 => 0,
    315 => 3000.0,
  ),
)

And if you're on php7.4 or higher, you can use array_map() with arrow syntax as a one-liner.

Code: (Demo)

var_export(array_map(fn($row) => array_replace($array1, $row),$array2));

Below php7.4 the array_map() syntax is considerably more verbose. Demo.

var_export(
    array_map(
        function($row) use ($array1) {
            return array_replace($array1, $row);
        },
        $array2
    )
);

Just for fun, here's an extra technique with nested looping (language constructs instead of function calls) and null coalescing (a kind of inline conditional expression).

Code: (Demo)

$merged = [];
foreach($array2 as $date => $row) {
    foreach ($array1 as $id => $value) {
        $merged[$date][$id] = $row[$id] ?? $value;
    }
}
var_export($merged);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your guide. I am still learning in programming. I don't have the idea to do it. I will look more on array_map()
1

You can use your first array like a template.

$newArray = [];
foreach($array2 as $date => $data) {
    $tmpArr = $array1;
    foreach ($data as $id=>$value) {
        if (!array_key_exists($id, $tmpArr)) {
            continue;
        }
        $tmpArr[$id] = $value;
    }
    $newArray[$date] = $tmpArr;
}

Comments

1

4 line solution. Take a look at comments in the code.

$final = []; //Creates an empty array.
foreach ($array2 as $key => $value) {
    $final[$key] = $array1; #adds array1 to each key of array2 on new array.
}
$merge = array_replace_recursive($final, $array2); #merges new array with array2.

var_dump($merge); #returns the solution on your question.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.