doutai1509 2017-05-16 21:04
浏览 16

如何在数组内的数组中获得唯一的第一个图像

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"
        }
      ]
    }
  ]
}
  • 写回答

2条回答 默认 最新

  • douzhanjia0773 2017-05-17 04:54
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据