0

My array looks like this. (see code below)

Lets say I want to access [value] in [visibiltiyindex] . And also [value] in [pagerank].

The code I came up with:

print_r ($results->answer->{"0"}->visibilityindex->{"0"}->value);
print_r ($results->answer->[0]->visibilityindex->[0]->value);
print_r ($results->answer->visibilityindex->value);

All code examples above wont work. I am using php 5.2, so array_map and array_column wont work for me.

I checked out this post and this one, but their methods dont work for me either since my array is even more multidimensional... Especially the [0] is irritating me.

My array:

stdClass Object
(
    [api_key] => Array
        (
            [0] => json
        )

    [method] => Array
        (
            [0] => domain.overview
        )

    [answer] => Array
        (
            [0] => stdClass Object
                (
                    [visibilityindex] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [domain] => spiegel.de
                                    [date] => 2014-03-17T00:00:00+01:00
                                    [value] => 372.4821 (I WANT THIS VALUE)
                                )

                        )

                    [pagerank] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [domain] => spiegel.de
                                    [date] => 2008-07-26T00:00:00+02:00
                                    [value] => 8  (I WANT THIS VALUE ALSO)
                                )

                        )

                    [pages] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [domain] => spiegel.de
                                    [date] => 2014-03-17T00:00:00+01:00
                                    [value] => 1130000
                                )

                        )

                    [age] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [domain] => spiegel.de
                                    [value] => 1996-12-23T00:00:00+01:00
                                )

                        )

                    [kwcount.seo] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [domain] => spiegel.de
                                    [date] => 2014-03-17T00:00:00+01:00
                                    [value] => 199022
                                )

                        )

                    [kwcount.sem] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [domain] => spiegel.de
                                    [date] => 2014-03-17T00:00:00+01:00
                                    [value] => 317
                                )

                        )

                )

        )

    [credits] => Array
        (
            [0] => stdClass Object
                (
                    [used] => 6
                )

        )

)

What can I do to only access those two values? Does it have to be a loop?

Community
  • 1
  • 1
ItsMeDom
  • 540
  • 3
  • 5
  • 18

1 Answers1

0

answer and visibilityIndex are arrays, so you need this:

$results->answer[0]->visibilityIndex[0]->value
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195