I want the result of my file display to be modified.
.
It shows the json file side by side but I want it to be combined
my code is:
<?php
$curlData = array(
array(
'url' => 'https://example.com/list',
'cookie' => 'session=example'
),
array(
'url' => 'https://anotherdomain.com/list',
'cookie' => 'session=another'
),
// Add more domains and cookies as needed
);
$responses = array();
foreach ($curlData as $data) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $data['url'],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
'Cookie: ' . $data['cookie']
),
));
$response = curl_exec($curl);
curl_close($curl);
$responses[] = $response;
}
// Output the responses
foreach ($responses as $response) {
echo $response;
}
?>
my result response json:
{
"success": true,
"msg": "",
"obj": [
{
"id": 1,
"remark": "D"
},
{
"id": 159,
"remark": "mssdss"
}
]
}
{
"success": true,
"msg": "",
"obj": [
{
"id": 14,
"remark": "tttt"
},
{
"id": 56,
"remark": "ok"
}
]
}
but i want merge result:
{
"success": true,
"msg": "",
"obj": [
{
"id": 1,
"remark": "D"
},
{
"id": 159,
"remark": "mssdss"
},
{
"id": 14,
"remark": "tttt"
},
{
"id": 56,
"remark": "ok"
}
]
}
OR
[
{
"id": 1,
"remark": "D"
},
{
"id": 159,
"remark": "mssdss"
},
{
"id": 14,
"remark": "tttt"
},
{
"id": 56,
"remark": "ok"
}
]
2
Answers
The sample data that you provided makes me think a simple merge of the
obj
key would suffice. (3v4l.org)This would be the resulting merged responses.
Please note that any duplicate keys in the second response would override them in the first response.
Edit: You could also do this in a longhanded fashion: