John Davidson

Merge associated array in php

0 comments
Message:


How I merge two array including the keys, below array structure available with requirement, can any one help for this:


$a1=array('533532'=>array('token'=>'token','tripboardrefid'=>'tripboardrefid'));

$a2=array('533179'=>array('token'=>'token','tripboardrefid'=>'tripboardrefid'));

print_r(array_merge($a1,$a2));

Getting this structure:


Array
(
[0] => Array
(
[token] => token
[tripboardrefid] => tripboardrefid
)

[1] => Array
(
[token] => token
[tripboardrefid] => tripboardrefid
)

)

Need this structure:


Array
(
[533179] => Array
(
[token] => token
[tripboardrefid] => tripboardrefid
)
[533532] => Array
(
[token] => token
[tripboardrefid] => tripboardrefid

)

)


My Answer:
>'John', '533533'=>'Doe', '533534'=>'Smith');
$a2=array('533532'=>'Doe', '533533'=>'Jane', '533534'=>'Smith');

To merge the two arrays including the keys, you can use the array_merge function in PHP like this:

$mergedArray = array_merge($a1, $a2);

This will result in the following merged array:

Array
(
[533532] => Doe
[533533] => Jane
[533534] => Smith
)

This will merge the two arrays while preserving the keys.

Rate this post

5 of 5 based on 5157 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us