Add two arrays in PHP

I have two arrays and I want to add it to make a combined one.

$first = Array
(
    [0] => Array
        (
            [countrySelected] => 3
            [stateSelected] => 176
            [citySelected] => 551
        )

)
$second = Array
(
    [0] => Array
        (
            [countrySelected] => 3
            [stateSelected] => N/A
            [citySelected] => N/A
        )

)

Now I want to make them a combined one:

Array
(
    [0] => Array
        (
            [countrySelected] => 3
            [stateSelected] => 176
            [citySelected] => 551
        )

    [1] => Array
        (
            [countrySelected] => 3
            [stateSelected] => N/A
            [citySelected] => N/A
        )
)

I tried it with array_merge but it combines them in one but I need two elements like [0], [1] as above.

Upvotes: 0

Views: 965

Answers (9)

Anuragh KP
Anuragh KP

Reputation: 788

I think you are not using the array_merge right way. I just create above array and I got the output like what you expect.

$arr1=[];
$arr1[0]=["countrySelected"=>3,"stateSelected"=>NULL,"citySelected"=>NULL];
print_r($arr1);

I get array below output

Array ( [0] => Array ( [countrySelected] => 3 [stateSelected] => [citySelected] => ) )

Then I just create another array like the second array in your question

$arr1=[];

$arr1[0]=["countrySelected"=>3,"stateSelected"=>NULL,"citySelected"=>NULL];


print_r($arr1);

I get array below output

Array ( [0] => Array ( [countrySelected] => 4 [stateSelected] => [citySelected] => ) )

and then I just merge the array

print_r(array_merge($arr1,$arr2));

and I got the below result

Array ( [0] => Array ( [countrySelected] => 3 [stateSelected] => [citySelected] => )

[1] => Array ( [countrySelected] => 4 [stateSelected] => [citySelected] => )

)

Upvotes: 0

shubhangee
shubhangee

Reputation: 539

Can you try with below code

$arr1=array('countrySelected'=>3,'stateSelected'=>176,'citySelected'=>551);
$arr2=array('countrySelected'=>3,'stateSelected'=>'N/A','citySelected'=>'N/A');

print_r(array($arr1, $arr2));

Upvotes: 0

Kishan Oza
Kishan Oza

Reputation: 1735

you can use array_merge_recursive check simple example Here

<?php
    $a1=array("a"=>"red","b"=>"green");
    $a2=array("c"=>"blue","b"=>"yellow");
    print_r(array_merge_recursive($a1,$a2));
?>

Upvotes: 0

Joe Alvini
Joe Alvini

Reputation: 299

$arrayone = array(WHATEVER VALUES ARE IN THIS ARRAY);
$arraytwo = array(WHATEVER VALUES ARE IN THIS ARRAY));

then combine the arrays like following

$arraythree = array($arrayone, $arraytwo);

var_dump($arraythree);

Upvotes: 1

dpattayath
dpattayath

Reputation: 160

You can use array_merge_recursive:

$arr1 = array(
        array(
                "countrySelected" => 3,
                "stateSelected" => 176,
                "citySelected" => 551
        )
);

$arr2 = array(
        array(
            "countrySelected" => 3,
            "stateSelected" => Null,
            "citySelected" => Null
        )
);

$arr3 = array_merge_recursive($arr1, $arr2);
print_r($arr3);

Result:
Array
(
[0] => Array
    (
        [countrySelected] => 3
        [stateSelected] => 176
        [citySelected] => 551
    )

[1] => Array
    (
        [countrySelected] => 3
        [stateSelected] =>
        [citySelected] =>
    )

)

Upvotes: 3

lazyCoder
lazyCoder

Reputation: 2561

Here you are, simply use array_map like below:

<?php
$array1 = array(
    array(
            "countrySelected" => 3,
            "stateSelected" => 176,
            "citySelected" => 551
        )

);
$array2 = array(
    array(
            "countrySelected" => 3,
            "stateSelected" => "N/A",
            "citySelected" => "N/A",
        )

);

array_map(function($arr) use(&$array1){
    $array1[] =  $arr;
}, $array2);

print_r($array1);

check output here https://paiza.io/projects/lJSL9E0IKJ1VF5IreDNmvA

Upvotes: 1

Aseem Upadhyay
Aseem Upadhyay

Reputation: 4537

My approach -

Case 1 - If you don't know how many keys will be present

$mergedArray = [];
foreach($array1 as $key=>$value)
{         $mergedArray[] = $value;    } //similarly for array 2

Case 2 - If there is only a single key present in the associative array

$mergedArray = [];
$mergedArray[] = $array1[0];
$mergedArray[] = $array2[0];

Upvotes: 0

Arash Younesi
Arash Younesi

Reputation: 1750

you can use two foreach loop for your 2 arrays and add them to third array.

foreach($arr1 as $item){
  $arr3[]=$item;
}
foreach($arr2 as $item){
  $arr3[]=$item;
}

Upvotes: -1

Bonestack
Bonestack

Reputation: 81

have you used array_push ? it could be something like:

array_push($Array2,$Array1[0]);

first parameter is the destination, the second is the element to insert.

array_push() insert it on the last field in array.

Upvotes: 2

Related Questions