How make group of the JSON data of same key

212
July 16, 2017, at 4:30 PM

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";
   }
}
Answer 1

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"
                }
            ]
        }
    ]
}'
Rent Charter Buses Company
READ ALSO
How to encrypt xml document type in browser game online? [on hold]

How to encrypt xml document type in browser game online? [on hold]

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:

259
update existing array record if already exists in database else insert into database using array

update existing array record if already exists in database else insert into database using array

i am getting error while updating my sales order using for each

286
Array reduce with associative array

Array reduce with associative array

I have an array like this

312