0

relatively new to coding. I'm currently having problems of parsing values from a multidimensional array

Here is the array:

Array
(
    [0] => Array
        (
            [title] => Wildfire
            [artist] => Rachel Platten
            [genre] => electropop
            [media] => Array
                (
                    [0] => Array
                        (
                            [totalDiscs] => 1
                            [position] => 1
                            [tracks] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Stand by You
                                            [number] => 1
                                            [artists] => Array
                                                (
                                                    [0] => Rachel Platten
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [title] => Astronauts
                                            [number] => 10
                                            [artists] => Array
                                                (
                                                    [0] => Rachel Platten
                                                )

                                        )

                                    [2] => Array
                                        (
                                            [title] => Congratulations
                                            [number] => 11
                                            [artists] => Array
                                                (
                                                    [0] => Rachel Platten
                                                )

                                        )

                                    [3] => Array
                                        (
                                            [title] => Superman
                                            [number] => 12
                                            [artists] => Array
                                                (
                                                    [0] => Rachel Platten
                                                )

                                        )

                                    [4] => Array
                                        (
                                            [title] => Lonely Planet
                                            [number] => 13
                                            [artists] => Array
                                                (
                                                    [0] => Rachel Platten
                                                )

                                        )

                                    [5] => Array
                                        (
                                            [title] => Stand by You (acoustic)
                                            [number] => 14
                                            [artists] => Array
                                                (
                                                    [0] => Rachel Platten
                                                )

                                        )

                                    [6] => Array
                                        (
                                            [title] => Speechless (acoustic)
                                            [number] => 15
                                            [artists] => Array
                                                (
                                                    [0] => Rachel Platten
                                                )

                                        )

                                    [7] => Array
                                        (
                                            [title] => Hey Hey Hallelujah
                                            [number] => 2
                                            [artists] => Array
                                                (
                                                    [0] => Rachel Platten feat. Andy Grammer
                                                )

                                        )

                                    [8] => Array
                                        (
                                            [title] => Speechless
                                            [number] => 3
                                            [artists] => Array
                                                (
                                                    [0] => Rachel Platten
                                                )

                                        )

                                    [9] => Array
                                        (
                                            [title] => Beating Me Up
                                            [number] => 4
                                            [artists] => Array
                                                (
                                                    [0] => Rachel Platten
                                                )

                                        )

                                    [10] => Array
                                        (
                                            [title] => Fight Song
                                            [number] => 5
                                            [artists] => Array
                                                (
                                                    [0] => Rachel Platten
                                                )

                                        )

                                    [11] => Array
                                        (
                                            [title] => Better Place
                                            [number] => 6
                                            [artists] => Array
                                                (
                                                    [0] => Rachel Platten
                                                )

                                        )

                                    [12] => Array
                                        (
                                            [title] => Lone Ranger
                                            [number] => 7
                                            [artists] => Array
                                                (
                                                    [0] => Rachel Platten
                                                )

                                        )

                                    [13] => Array
                                        (
                                            [title] => You Don’t Know My Heart
                                            [number] => 8
                                            [artists] => Array
                                                (
                                                    [0] => Rachel Platten
                                                )

                                        )

                                    [14] => Array
                                        (
                                            [title] => Angels in Chelsea
                                            [number] => 9
                                            [artists] => Array
                                                (
                                                    [0] => Rachel Platten
                                                )

                                        )

                                )

                        )

                )

            [types] => Array
                (
                    [0] => Album
                    [1] => Official
                )

            [score] => 1
        )

)

and this is my code:

$json = json_decode($result, true);



   echo 'Title: ' . $json['title'] . '<br />';
    echo 'Artist: ' . $json['artist'] . '<br />';
    echo 'Genre: ' . $json['genre'] . '<br />';

However every time i debug it would come out a notice stating :

E_NOTICE : type 8 -- Undefined index: title -- at line 34 Title:
E_NOTICE : type 8 -- Undefined index: artist -- at line 35 Artist:
E_NOTICE : type 8 -- Undefined index: genre -- at line 36 Description:

what is the problem with my code? and how do i fix it?

David Teh
  • 43
  • 5
  • @PaulCrovella I don't think so. i did a var_dump to check the array for hidden characters but found none. So i do not get what my code is not executing. – David Teh Sep 24 '16 at 15:13
  • Because you're not paying attention to the structure of it. E.g. `$json[0]['title']` – user3942918 Sep 24 '16 at 15:16

1 Answers1

0

you are running with three dimensional array.. you 'll get the answer by writing like this,...

echo 'Title: ' . $json['media']['tracks']['title'] . '<br />';

please be little bit conscious while passing names inside array.

Jaccs
  • 942
  • 3
  • 10
  • 31