1

所以我在访问多维数组中的值时遇到问题。这是我的 $klasemen->data 的 print_r 结果/数组结构:

Array (
[0] => stdClass Object
    (
        [team] => stdClass Object
            (
                [id] => 5055
                [name] => Manchester United
            )

        [breadcrumb] => Array
            (
                [0] => stdClass Object
                    (
                        [id] => 1
                        [name] => Bola
                    )

                [1] => stdClass Object
                    (
                        [id] => 2
                        [name] => Internasional
                    )

                [2] => stdClass Object
                    (
                        [id] => 23
                        [name] => Liga Premiere
                    )

            )

        [games_played] => 23
        [wins] => 18
        [draws] => 2
        [losses] => 3
        [goals_scored] => 57
        [goals_against] => 30
        [goals_difference] => 27
        [points] => 56
    )

[1] => stdClass Object
    (
        [team] => stdClass Object
            (
                [id] => 5057
                [name] => Manchester City
            )

        [breadcrumb] => Array
            (
                [0] => stdClass Object
                    (
                        [id] => 1
                        [name] => Bola
                    )

                [1] => stdClass Object
                    (
                        [id] => 2
                        [name] => Internasional
                    )

                [2] => stdClass Object
                    (
                        [id] => 23
                        [name] => Liga Premiere
                    )

            )

        [games_played] => 23
        [wins] => 15
        [draws] => 6
        [losses] => 2
        [goals_scored] => 45
        [goals_against] => 19
        [goals_difference] => 26
        [points] => 51
    )

我想访问比赛的名称(例如 Liga Premiere)并将其打印在我的视图上,但我不断收到"Message: Trying to get property of non-object"。我认为语法或我在面包屑数组中访问数组的方式有问题。我是新手,我对这个多维数组的东西感到困惑 T_T

这是我想打印值时视图上的行代码(我很确定它有问题)。

echo $klasemen->数据->面包屑[2]->名称

无论如何 $klasemen 是包含所有数据的变量。任何人都可以帮助我吗?谢谢!:-)

4

3 回答 3

1

尝试使用这个泡芙

$klasemen->data->breadcrumb[2]->name

您只需要更改 $klasemen->data 的 $klasemen[1] 因为它是一个数组。您的对象中不存在 data 属性。

解决方案:

$klasemen[1]->breadcrumb[2]->name
于 2013-01-23T11:14:03.407 回答
0

您对data. 试试这个:

echo $klasemen[0]->breadcrumb[2]->name
于 2013-01-22T16:24:23.093 回答
0

以下代码:

echo $klasemen->data->breadcrumb[2]->name

相当于:

echo $klasemen[0]->breadcrumb[2]->name

您当前正在处理对象数组。并且您需要提供索引来指定当前对象项。

于 2013-01-23T11:16:57.473 回答