简体   繁体   中英

Merge two 2 Dimensional json in php

I have two Two Dimensional json.

[{"4":"213231.jpg"},{"5":"Cadbury 5 star Chocolate.jpg"}]

[{"1":"slider-1.png"},{"2":"slider-2.png"},{"3":"slider-3.png"},{"4":"slider-4.png"},{"5":"slider-5.png"}]

i want to merge this two arrays and i want the answer as

[{"1":"slider-1.png"},{"2":"slider-2.png"},{"3":"slider-3.png"},{"4":"213231.jpg"},{"5":"Cadbury 5 star Chocolate.jpg"}]

Try following:

$json1 = '[{"1":"slider-1.png"},{"2":"slider-2.png"},{"3":"slider-3.png"},{"4":"slider-4.png"},{"5":"slider-5.png"}]';
$json2 = '[{"4":"213231.jpg"},{"5":"Cadbury 5 star Chocolate.jpg"}]';

echo json_encode(array_merge(json_decode($json1), json_decode($json2)));

Output

[{"1":"slider-1.png"},{"2":"slider-2.png"},{"3":"slider-3.png"},{"4":"slider-4.png"},{"5":"slider-5.png"},{"4":"213231.jpg"},{"5":"Cadbury 5 star Chocolate.jpg"}]

Try this :

$json1 = '[{"1":"slider-1.png"},{"2":"slider-2.png"},{"3":"slider-3.png"},{"4":"slider-4.png"},{"5":"slider-5.png"}]';
$json2 = '[{"4":"213231.jpg"},{"5":"Cadbury 5 star Chocolate.jpg"}]';

$json1 = json_decode($json1);
unset($json1[4],$json1[5]);
$json1 = json_encode($json1);
echo json_encode(array_merge(json_decode($json1), json_decode($json2)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM