Home » Php » php – How to merge values from one array into another one?

php – How to merge values from one array into another one?

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have two arrays and need to join or merge them into one.

        $array1 = array(
            array("Account 1", "EUR", "100", "333"),
            array("Account 2", "EUR", "200", "444")
            );

        $array2 = array(
            array("Account 1", "EUR", "100", "111"),
            array("Account 2", "EUR", "200", "222")
            );

        $array = array_merge($array1,$array2);

Then my outcome is:

[["Account 1","EUR","100","333"],["Account 2","EUR","200","444"],["Account 1","EUR","100","111"],["Account 2","EUR","200","222"]]

I would like to have like this:

[["Account 1","EUR","100","333","111"],["Account 2","EUR","200","444","222"]]

How to make it? Any advice would be appreciated.

How to&Answers:

It looks like the program should append the last element of array2[i] to array1[i]. You’ll need a loop of course, and you may find PHP: end helpful. If that is not the intent of the program, then be sure to update the question with a more accurate problem statement or example.

Answer:

This is how I resolved the problem:

for ($i = 0; $i < count($array1); $i++) {
  $array1[$i][]=$array2[$i][3];
}