如何从嵌套数组中提取特定对象?

时间:2014-08-08 19:00:26

标签: php json

我正在使用Behance API(https://github.com/behance/network_api_php)来尝试提取项目的图像。

我已经能够成功编写项目名称和URL的代码,但封面图片让我感到难过。

我的PHP代码中有这个..

<?php
foreach($api->getUserProjects( 'username' ) as $beproj): ?>
<h1><?php echo $beproj->name; ?></h1>
<a href="<?php echo $beproj->url; ?>"><?php echo $beproj->name; ?></a><br />
  <img src="<?php echo $beproj->covers[202]; ?>"> 
<?php endforeach; ?>

这样可以完美地输出名称和URL,但202像素大小的封面图像不会显示出来。有人可以告诉我提取202图像的正确方法吗?

Behance API中的数组看起来像这样......

Array
(
[0] => stdClass Object
    (
        [id] => 18848121
        [name] => Sketchbook
        [published_on] => 1407253953
        [created_on] => 1407251608
        [modified_on] => 1407451145
        [url] => https://www.behance.net/gallery/18848121/Sketchbook
        [privacy] => public
        [fields] => Array
            (
                [0] => Graphic Design
                [1] => Icon Design
                [2] => Illustration
            )

        [covers] => stdClass Object
            (
                [404] => https://m1.behance.net/rendition/projects/18848121/404/2e0cadfd1167f2bdf89decce172e5e8f.jpeg
                [202] => https://m1.behance.net/rendition/projects/18848121/orig/2e0cadfd1167f2bdf89decce172e5e8f.jpeg
                [230] => https://m1.behance.net/rendition/projects/18848121/230/2e0cadfd1167f2bdf89decce172e5e8f.jpeg
                [115] => https://m1.behance.net/rendition/projects/18848121/115/2e0cadfd1167f2bdf89decce172e5e8f.jpeg
            )

        [mature_content] => 0
        [mature_access] => allowed
        [owners] => Array
            (
                [0] => stdClass Object
                    (
                        [id] => 215057
                        [first_name] => Mike
                        [last_name] => 
                        [username] => creativemints
                        [city] => Prague
                        [state] => 
                        [country] => Czech Republic
                        [location] => Prague, Czech Republic
                        [company] => Creative Mints
                        [occupation] => 
                        [created_on] => 1281693887
                        [url] => https://www.behance.net/creativemints
                        [images] => stdClass Object
                            (
                                [50] => https://mir-cdn.behance.net/v1/rendition/user/50/215057.53ac6f3dacec6.jpg
                                [115] => https://mir-cdn.behance.net/v1/rendition/user/115/215057.53ac6f3dacec6.jpg
                                [138] => https://mir-cdn.behance.net/v1/rendition/user/138/215057.53ac6f3dacec6.jpg
                            )

                        [display_name] => Mike
                        [fields] => Array
                            (
                                [0] => Illustration
                                [1] => UI/UX
                                [2] => Game Design
                            )

                    )

            )

        [stats] => stdClass Object
            (
                [views] => 4510
                [appreciations] => 1307
                [comments] => 65
            )

        [for_sale] => 0
    )

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您正在以数组的形式访问“封面”,但事实上,它是一个对象。

[covers] => stdClass Object

怎么样:

echo $beproj->covers->{'202'}

答案 1 :(得分:0)

答案很简单..我需要使用

$beproj->covers->{'202'} 

而不是

$beproj->covers->['202']