CodeIgniter - 访问多维数组

时间:2013-01-22 16:21:47

标签: arrays codeigniter multidimensional-array

所以我在访问多维数组中的值时遇到问题。这是我的$ klasemen->数据的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)并将其打印在我的视图上,但我一直在“消息:试图获得非对象的属性”。我相信在Breadcrumb数组中访问数组的语法或方式有问题。我是新手,我对这个多维数组事件T_T

感到困惑

当我想要打印值时,我的视图中的行代码(我很确定它有问题)。

  

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

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

3 个答案:

答案 0 :(得分:1)

尝试使用此paulfah

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

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

解决方案:

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

答案 1 :(得分:0)

您对data的引用不当。试试这个:

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

答案 2 :(得分:0)

以下代码:

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

相当于:

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

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