paulfah
paulfah

Reputation: 113

CodeIgniter - Accessing multi-dimentional array

So I have problem in accessing value in multidimentional array. Here's print_r result/array structure of my $klasemen->data:

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
    )

I want to access the name of competition (eg. Liga Premiere) and print it on my view but I keep getting "Message: Trying to get property of non-object". I believe something's wrong with the syntax or my way accessing array in Breadcrumb array. I'm new and I get confused with this multidimentional array thing T_T

Here's the line code on my view when I want to print the value (I'm pretty sure something's wrong with it).

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

Anyway $klasemen is variable that contains all data. Anyone can help me with this? Thank you! :-)

Upvotes: 1

Views: 152

Answers (3)

Andy Davies
Andy Davies

Reputation: 4287

Try using this paulfah

You

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

You just need to change $klasemen->data for $klasemen[1] as it is an array. The data property does not exist in your object.

Solution:

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

Upvotes: 1

hsuk
hsuk

Reputation: 6860

The following code :

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

is equivalent to :

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

You are currently dealing with array of objects. And you need to provide index to specify the current object item.

Upvotes: 0

Justin Ethier
Justin Ethier

Reputation: 134177

You have a bad reference to data. Try this:

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

Upvotes: 0

Related Questions