-1

i am trying to merge two similar arrays with the same keys

Array
(
    [0] => 4064628
    [1] => 4064953
    [2] => 4064922
    [3] => 4064870
    [4] => 4064789
    [5] => 4064631
    [6] => 4065044
    [7] => 4064942
    [8] => 4064938
    [9] => 4064936
)
Array
(
    [0] => 165020
    [1] => 165026
    [2] => 165025
    [3] => 165023
    [4] => 165024
    [5] => 165021
    [6] => 165027
    [7] => 165043
    [8] => 165042
    [9] => 165045
)

but when i use array_merge or array_merge_recursive gives the same output:

Array
(
    [0] => 4064628
    [1] => 4064953
    [2] => 4064922
    [3] => 4064870
    [4] => 4064789
    [5] => 4064631
    [6] => 4065044
    [7] => 4064942
    [8] => 4064938
    [9] => 4064936
    [10] => 165020
    [11] => 165026
    [12] => 165025
    [13] => 165023
    [14] => 165024
    [15] => 165021
    [16] => 165027
    [17] => 165043
    [18] => 165042
    [19] => 165045
)

but i try to get something like this:

Array
(
    [0] => Array
        (
            [0] => 4064628
            [1] => 165020
        )

    [1] => Array
        (
            [0] => 4064935
            [1] => 165026
        )

    [2] => Array
        (
            [0] => 4064922
            [1] => 165025
        )
     .......

can someone help please to merge these two arrays? this seems so simple but there is something i don't get, and i don't know what

4
  • $zipped = array_map(null, $array1, $array2); Commented Jul 28, 2022 at 11:33
  • nope, i submited the solution from Noe CB, it worked for me Commented Jul 28, 2022 at 12:15
  • You are asking for "array transposition" -- this has been been asked roughly 100 times on Stack Overflow. Commonly devs are passing in one array that needs to be "spread". In your case, you just pass the two arrays in individually. 3v4l.org/QKLLg Commented Jul 28, 2022 at 14:34
  • @billyonecan please help to close questions when you know that they are duplicates. By not closing, you allowed new users to waste their time providing redundant content. Commented Jul 28, 2022 at 14:35

2 Answers 2

0
$output_arr=[];
foreach ($array1 as $key => $value) {
$output_arr[]=[$value,$array2[$key]];}
Sign up to request clarification or add additional context in comments.

Comments

0
$arr=[];
for ($i=0;$i<count($arr1);$i++){
    array_push($arr, [$arr1[$i], $arr2[$i]]);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.