-2

Here i have one array(first array) inside i have one more array(second array), now i want to display only first image from second array(galleryImages) , how can do this. i tried but i am not able to get the results

print_r($response);

 Array
(
    [0] => stdClass Object
        (
            [gallery_id] => 2
            [title] => Annual Day 2017
            [description] => 
            [galleryImages] => ["1.jpg","2.jpg","3.jpg","4.jpg"]
            [reg_on] => 2017-05-17 01:55:12
            [created_by] => rajeshdash123@gmail.com
            [school_id] => 2
            [status] => 0
        )

    [1] => stdClass Object
        (
            [gallery_id] => 3
            [title] => Sports Day
            [description] => 
            [galleryImages] => ["1.jpg","2.jpg","3.jpg"]
            [reg_on] => 2017-05-17 01:55:36
            [created_by] => rajeshdash123@gmail.com
            [school_id] => 2
            [status] => 0
        )

)

Expected Results

 {
  "status": "Success",
  "data": [
    {
      "gallery_id": "2",
      "title": "Annual Day 2017",
      "description": "",
      "galleryImagesCount": 4,
      "gallery":"1.jpg"
    },
    {
      "gallery_id": "3",
      "title": "Sports Day 2017",
      "description": "",
      "galleryImagesCount": 4,
      "gallery":"1.jpg"
    }
  ],
}

I tried like this but is i am not getting the exact results

$images = array();
foreach ($response as $key => $value)
{
    $img['gallery_id'] = $value->gallery_id;
    $img['title'] = $value->title;
    $img['description'] = $value->description;
    $img['galleryImagesCount'] = count(json_decode($value->galleryImages,true));
    $img['gallery'] = json_decode($value->galleryImages,true);
    array_push($images,$img);

}

$return=array('status'=>"Success",'Images'=>$images);
echo json_encode($return);

Getting Results

    {
  "status": "Success",
  "Images": [
    {
      "gallery_id": "2",
      "title": "Annual Day 2017",
      "description": "",
      "galleryImagesCount": 4,
      "gallery": [
        "d17ac9d0aeb6435eaa294e0d69d4cc8f.jpg",
        "a91945e0cf55379f51cf5faef10d7a4a.jpg",
        "2d1501045ddbb3ccc238e70f9af05027.jpg",
        "071c3b5f969bed1d1e2ee4b6531e4444.jpg"
      ]
    },
    {
      "gallery_id": "3",
      "title": "Sports Day",
      "description": "",
      "galleryImagesCount": 4,
      "gallery": [
        "f0ba574fd46a01ff5a41855a97c710ca.jpg",
        "1d10802f1b74e660117f36bd6dd0aa26.jpg",
        "e705fb66f767a1b914200ca8d3cae700.jpg",
        "3d5d8828331e13d3decc94021a64e5ca.jpg"
      ]
    }
  ]
}

Here what happening means gallery is coming an array , for me don't want array i need first image only, please check my expected results, update the answer

  • Updated expected results
{
  "status": "Success",
  "Images": [
    {
      "gallery_id": "2",
      "title": "Annual Day 2017",
      "description": "",
      "galleryImagesCount": 4,
      "gallery": [
        {
       "galleryimage": "1.jpg"
        },
        {
       "galleryimage": "2.jpg"
        }
      ]
    },
    {
      "gallery_id": "3",
      "title": "Sports Day",
      "description": "",
      "galleryImagesCount": 4,
       "gallery": [
        {
       "galleryimage": "1.jpg"
        },
        {
       "galleryimage": "2.jpg"
        }
      ]
    }
  ]
}
Swamy m
  • 7
  • 2
  • 8
  • 2
    `$img['gallery'] = json_decode($value->galleryImages,true)[0];` for php5.4 and up. – u_mulder May 16 '17 at 21:07
  • Ya i got it, thanks – Swamy m May 16 '17 at 21:14
  • Mr @ u_mulder ,I have one doubt , here gallery is displaying in array, how to make object in gallery, please check my updated expected results – Swamy m May 16 '17 at 21:30
  • check this is to make array become object http://stackoverflow.com/questions/1869091/how-to-convert-an-array-to-object-in-php – Kelvin May 17 '17 at 01:32
  • 4
    Please reserve the use of blockquote formatting (`>`) for quotations. Do not use it to "highlight" sections. You don't need formatting there at all; this should be set in plain text. – Funk Forty Niner Aug 23 '17 at 18:34

2 Answers2

0

Instead of [galleryImages] => ["1.jpg","2.jpg","3.jpg"], use for loop to iterate through galleryImages.

Create an associative array with key =>value pair like [galleryImages] => ["galleryimage1" => "1.jpg", "galleryimage2" => "2.jpg", "galleryimage3" => "3.jpg"].

While json_decode you will get intended output.

0

My changes and explanations are in the code block:

Code: (Demo)

// assumed that previous line was something like $response=json_decode($json);
$response=[
    (object)[
        'gallery_id'=>2,
        'title'=>'Annual Day 2017',
        'description'=>'', 
        'galleryImages'=>["1.jpg","2.jpg","3.jpg","4.jpg"],
        'reg_on'=>'2017-05-17 01:55:12',
        'created_by'=>'rajeshdash123@gmail.com',
        'school_id'=>2,
        'status'=>0
    ],
    (object)[
        'gallery_id'=>3,
        'title'=>'Sports Day',
        'description'=>'', 
        'galleryImages'=>["1.jpg","2.jpg","3.jpg"],
        'reg_on'=>'2017-05-17 01:55:36',
        'created_by'=>'rajeshdash123@gmail.com',
        'school_id'=>2,
        'status'=>0
    ]
];

foreach ($response as $value){  // removed $key=> because it was unnecessary
    $img['gallery_id'] = $value->gallery_id;
    $img['title'] = $value->title;
    $img['description'] = $value->description;
    $img['galleryImagesCount'] = count($value->galleryImages);  // removed json_decode()
    $img['gallery'] = $value->galleryImages[0];  // removed json_decode and added [0] to access first
    $images[]=$img;  // swapped push() call with identical function-less "push"
}

if(isset($images)){  // added this condition to ensure there was something to return
    $return=array('status'=>"Success",'Images'=>$images);
    //var_export($return);
    echo json_encode($return);
}else{
    // enter some sort of error message / default behavior
}

Output:

{"status":"Success","Images":[{"gallery_id":2,"title":"Annual Day 2017","description":"","galleryImagesCount":4,"gallery":"1.jpg"},{"gallery_id":3,"title":"Sports Day","description":"","galleryImagesCount":3,"gallery":"1.jpg"}]}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136