如何从数组中获取字段

时间:2015-03-05 18:21:16

标签: php arrays

我有一个类似下面的数组

   Response Data: Array
(
    [0] => Array
        (
            [offer_id] => 1370
            [Thumbnail] => Array
                (
                    [48286] => Array
                        (
                            [id] => 48286
                            [offer_id] => 1370
                            [display] => gream-2.jpg
                            [filename] => gream-2.jpg
                            [size] => 14014
                            [status] => active
                            [type] => offer thumbnail
                            [width] => 100
                            [height] => 50
                            [code] => 
                            [flash_vars] => 
                            [interface] => network
                            [account_id] => 
                            [is_private] => 0
                            [created] => 2015-02-16 08:28:59
                            [modified] => 0000-00-00 00:00:00
                            [url] => http://media.vcommission.com/brand/files/vcm/1370/gream-2.jpg
                            [thumbnail] => http://media.vcommission.com/brand/files/vcm/1370/thumbnails_100/gream-2.jpg
                        )

                )

        )

)

我已通过以下代码填充此数组:

  if($apiResponse['response']['status'] === 1) {
        // No errors encountered
        echo 'API call successful';
        echo PHP_EOL;
        echo 'Response Data: ' . print_r($apiResponse['response']['data'], true);

        echo PHP_EOL;
    }

现在我想从数组中获取[url]并在此处执行此操作是我的努力:

foreach ($apiResponse['response']['data'] as $data) {


   echo '<img src="'.$data['id']['url'].'">';   

}

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

如果这不起作用,请尝试详细说明它是如何构建的。

foreach ($apiResponse['response']['data'] as $data) {

 $urls = array();
 foreach($data['Thumbnail'] AS $thumbnail){
  array_push($urls, $thumbnail['url']);
 }

 /* All urls if there are several */
 foreach($urls AS $url){
  echo '<img src="'.$url.'">';
 }

 /* Or only first */
 echo '<img src="'.$urls[0].'">';
}

[编辑]
我不明白为什么它不起作用,你有任何错误吗?尝试逐步打印出来; foreach之后的$ urls,$ data ['缩略图'] ......