如何遍历这个对象PHP

时间:2017-02-10 14:19:26

标签: php

我只是打印facebook api resonse

print_r($response)

输出

Facebook\GraphNodes\GraphEdge Object
(
[request:protected] => Facebook\FacebookRequest Object
    (
        [app:protected] => Facebook\FacebookApp Object
            (
                [id:protected] => secret
                [secret:protected] => secret
            )

        [accessToken:protected] => secret
        [method:protected] => GET
        [endpoint:protected] => /search?q=co&type=page
        [headers:protected] => Array
            (
                [Content-Type] => application/x-www-form-urlencoded
            )

        [params:protected] => Array
            (
            )

        [files:protected] => Array
            (
            )

        [eTag:protected] => 
        [graphVersion:protected] => v2.5
    )

[metaData:protected] => Array
    (
        [paging] => Array
            (
                [cursors] => Array
                    (
                        [before] => MAZDZD
                        [after] => MjQZD
                    )

                [next] => https://graph.facebook.com/v2.8/search?access_token=EAAafvw8PPA4BACHY8V6GDpbzMbtRlZC7dZCRnOGtO26Yc4g4yWWvqZCsMBPOWO3b72n2JPjXP8KD91ZCMXMAcARGUsk5cNShhy5LxOmj0Gz4ZA2ESzPZAd4VzBCpdZATCZBvZCOkAIxBd1gXBzkMY0DheyjruSlMHEPbbuVuTY350wgZDZD&q=co&type=page&limit=25&after=MjQZD
            )

    )

[parentEdgeEndpoint:protected] => 
[subclassName:protected] => 
[items:protected] => Array
    (
        [0] => Facebook\GraphNodes\GraphNode Object
            (
                [items:protected] => Array
                    (
                        [name] => SC Corinthians Paulista
                        [id] => 132769576762243
                    )

            )

        [1] => Facebook\GraphNodes\GraphNode Object
            (
                [items:protected] => Array
                    (
                        [name] => Miranda Cosgrove
                        [id] => 9934379006
                    )

我想访问[items:protected]数组。但我找不到如何获取

我试过

$ items_array = $ response-> items:protected;

但这不起作用,请帮助我。我想得到那个数组并迭代它

4 个答案:

答案 0 :(得分:2)

您可以使用几种不同的技术遍历此对象:

(推荐技术:)您可以使用GraphNode对象的方法通过循环GraphEdge对象来一次性输出项目以获取GraphNode并在其上调用'asJson()'或'asArray()':

foreach($response as $node){
    //Print as json
    print($node->asJson());

    //Print as array
    print_r($node->asArray());
}

还有其他方法可以访问GraphNode对象属性,例如'getField'和'getFieldNames',如果你只想获取一个特定的数据项而不想返回整个东西(你可以在这里阅读它们) :https://developers.facebook.com/docs/php/GraphNode/5.0.0

(也可以:)或者你可以使用嵌套的foreach循环(根据这里的文档:https://developers.facebook.com/docs/php/GraphEdge/5.0.0)。

有些事情:

foreach ($response as $node) {
    //each item returned by response
    foreach ($node as $itemKey => $itemValue) {
        //each item within the node
        print $itemKey.' is '.$itemValue;
    }
}

答案 1 :(得分:1)

如果受到保护,则表示您无法从外部访问。如果有一个提供项目的公共方法,我建议你看一下Facebook \ FacebookRequest类,否则你无法访问它。

答案 2 :(得分:1)

Facebook php api建议简单foreach来获取节点:

foreach ($response as $graphNode) {
    print_r($graphNode);
}

如何使用GraphNode描述的SELECT DISTINCT CHANNELS.ChanId, CHANNELS.name FROM CHANNELS JOIN SUBSCRIBERS on CHANNELS.ChanId = SUBSCRIBERS.ChanID WHERE CHANNELS.type = 'public' AND SUBSCRIBERS.nickname !='Jonny' 对象。

答案 3 :(得分:0)

你可以在facebook github repo中找到这个对象的代码来源:Facebook/GraphNodes/GraphEdge

GraphEdge扩展Collection,实现ArrayAccess和IteratorAggregate

迭代器是在$ items属性上制作的......

/**
 * Get an iterator for the items.
 *
 * @return ArrayIterator
 */
public function getIterator()
{
    return new ArrayIterator($this->items);
}

然后是的,使用简单的foreach应该可以正常工作