How make group of the JSON data of same key

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 php 

$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";
   }
}

 

Asked on July 16, 2017 in php.
Add Comment



  • 0 Answer(s)



  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.