I am working in php.I have the JSON data in which muliple data comes some data have two same keys as shown below
{
"statusCode": 800,
"status": "Sucess",
"result": [
{
"id": "1",
"imgId": "1",
"gallery_name": "Schools DNA Gallery",
"gallery_desc": "Gallery desc for Demo public senior secondary school-1 Status : published",
"gallery_status": "1",
"image_name": "sdna_gallery_img_5662a31fb4ca9.jpg",
"image_tittle": "2.jpg",
"image_url": "assets/uploads/school/410920433/gallery/10803076"
},
{
"id": "1",
"imgId": "3",
"gallery_name": "Schools DNA Gallery",
"gallery_desc": "Gallery desc for Demo public senior secondary school-1 Status : published",
"gallery_status": "1",
"image_name": "sdna_gallery_img_5662a469c88aa.jpg",
"image_tittle": "Gallery-img-1-updated here",
"image_url": "assets/uploads/school/410920433/gallery/10803076"
}
]
}
How display data like
{
"statusCode": 800,
"status": "Sucess",
"result": [
{
"id": "1",
"imgId": "1",
"gallery_name": "Schools DNA Gallery",
"gallery_desc": "Gallery desc for Demo public senior secondary school-1 Status : published",
"gallery_status": "1",
"items": [
{
"image_name": "sdna_gallery_img_5662a31fb4ca9.jpg",
"image_tittle": "2.jpg",
"image_url": "assets/uploads/school/410920433/gallery/10803076"
},
{
"image_name": "sdna_gallery_img_5662a469c88aa.jpg",
"image_tittle": "Gallery-img-1-updated here",
"image_url": "assets/uploads/school/410920433/gallery/10803076"
}
]
}
]
}
My means make an array of same key values. By a little search i found that this may possible with the foreach loop but how implement with multiple keys dont know. I tried with the code. But dont get the optimal solution
$items=json_decode($json,true);
foreach ($items as $item) {
foreach ($item as $key => $value)
{
if( is_array($value) ) {
foreach ($value as $subkey => $part)
{
foreach ($part as $partkey => $partvalue)
{
echo $key .' '. $subkey . ' ['.$partkey.'] => '.$partvalue."\n";
}
}
} else {
echo $key.' => '.$value."\n";
}
}
Code: (Demo)
$json='{
"statusCode": 800,
"status": "Sucess",
"result": [
{
"id": "1",
"imgId": "1",
"gallery_name": "Schools DNA Gallery",
"gallery_desc": "Gallery desc for Demo public senior secondary school-1 Status : published",
"gallery_status": "1",
"image_name": "sdna_gallery_img_5662a31fb4ca9.jpg",
"image_tittle": "2.jpg",
"image_url": "assets/uploads/school/410920433/gallery/10803076"
},
{
"id": "1",
"imgId": "3",
"gallery_name": "Schools DNA Gallery",
"gallery_desc": "Gallery desc for Demo public senior secondary school-1 Status : published",
"gallery_status": "1",
"image_name": "sdna_gallery_img_5662a469c88aa.jpg",
"image_tittle": "Gallery-img-1-updated here",
"image_url": "assets/uploads/school/410920433/gallery/10803076"
}
]
}';
$array=json_decode($json,true);
foreach($array['result'] as $a){
if(!isset($result[$a['id']])){
$result[$a['id']]=array_slice($a,0,5); // only retain the first subarray's first 5 identifying elements
}
$result[$a['id']]['items'][]=array_slice($a,5); // push last 3 elements from each subarray
}
$result=array_slice($array,0,2)+["result"=>array_values($result)]; // re-index subarrays, and prepend the first two elements
var_export(json_encode($result));
Output:
'{
"statusCode":800,
"status":"Sucess",
"result":[
{
"id":"1",
"imgId":"1",
"gallery_name":"Schools DNA Gallery",
"gallery_desc":"Gallery desc for Demo public senior secondary school-1 Status : published",
"gallery_status":"1",
"items":[
{
"image_name":"sdna_gallery_img_5662a31fb4ca9.jpg",
"image_tittle":"2.jpg",
"image_url":"assets\\/uploads\\/school\\/410920433\\/gallery\\/10803076"
},
{
"image_name":"sdna_gallery_img_5662a469c88aa.jpg",
"image_tittle":"Gallery-img-1-updated here",
"image_url":"assets\\/uploads\\/school\\/410920433\\/gallery\\/10803076"
}
]
}
]
}'
Firebase Cloud Functions: PubSub, "res.on is not a function"
TypeError: Cannot read properties of undefined (reading 'createMessageComponentCollector')
i have a php file that can decrypt xml/conf document typeBut when i re-encrypt them again, it's fail!! Could you show me where is wrong?? Here my code:
I have URL pattern like thisI managed this URL pattern through
i am getting error while updating my sales order using for each