如何回显此对象的值?

时间:2017-09-26 09:29:57

标签: php arrays object

当我打印我的物体时,这就是它的样子:

stdClass Object
(
    [data] => Array
        (
            [0] => stdClass Object
                (
                    [name] => post_impressions
                    [period] => lifetime
                    [values] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [value] => 4448
                                )

                        )

                    [title] => Lifetime Post Total Impressions
                    [description] => Lifetime: The number of impressions of your Page post. (Total Count)
                    [id] => 367712570004555_1292256510883485/insights/post_impressions/lifetime
                )

        )

    [paging] => stdClass Object
        (
            [previous] => https://graph.facebook.com/v2.10/3677125700055_129256510883485/insights?access_token=EAACEdEse0cBANjKErkMLhCgmZBBrgLn8KtZBW2d6JbWz7sZAivld67JxvP8MqyZCs9xg3Bh11lzETwd64LAPp3zbstu8ELFZC8HOwYyxckqJ60WQcVL1FqB3iy5L3oRCsFSQzPx2B53WZCMwRTSGKyqXe8uFGPhMZBnFYsZCFHyaUA7MzHGgcqIYxsjAlJomYrOMZCqo6PwZDZD&metric=post_impressions&since=1505890800&until=1506063600
            [next] => https://graph.facebook.com/v2.10/36771257004555_1292256583485/insights?access_token=EAACEdEose0cBANjLn8KtZBW2d6JbWz7sZAivld67JxvP8MqyZCs9xg3Bh11lzETOwd64LIpAPp3zbstu8ELFZC8HOwYyxcVL1FqB3iy5L3oRCsFSQzPx2B53WZCMwRTSGKyqXe8uFGPhMZBnFYsZCFHyaUA7MzHGgcqIYxsjAlJomYrOMZCqo6PwZDZD&metric=post_impressions&since=150626400&until=150409200
        )

)

我如何回应[value]

我试过

echo $insights[0]->data->values->value;但这给了我一个Fatal error: Cannot use object of type stdClass as array in /home/studione/public_html/includes/facebook.php on line 83

我也尝试过:echo $insights->data->values->value;但这没有显示任何内容,甚至没有错误。

3 个答案:

答案 0 :(得分:7)

data是一个数组,values也是一个数组。如果要输出元素,则必须指定这些数组的正确索引:

echo $insights->data[N]->values[M]->value;

其中N和M是对象内部数组的索引。

答案 1 :(得分:5)

试试这个:

def get_context_data(self, **kwargs):
    context = super(BaseView, self).get_context_data(**kwargs)
    context['object_list'] = sorted(
        itertools.chain(Document.objects.all(), Program.objects.all()),
        key=lambda x: x.validity_date
    )
    return context

答案 2 :(得分:-1)

我认为你错过了零,试着把它包起来:

<?php  $insights->data->values->{0}->value;?>