未定义的索引:media //尝试解析数组键中的值时

时间:2016-09-24 14:06:12

标签: php arrays parsing error-handling

编码相对较新。我目前遇到了从多维数组中解析值的问题

这是数组:

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
        )

)

这是我的代码:

$json = json_decode($result, true);



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

然而,每次调试时都会出现一个通知:

E_NOTICE:类型8 - 未定义索引:标题 - 第34行标题:
E_NOTICE:类型8 - 未定义索引:艺术家 - 第35行艺术家:
E_NOTICE:类型8 - 未定义索引:流派 - 在第36行描述:

我的代码有什么问题?以及如何解决它?

1 个答案:

答案 0 :(得分:0)

你正在使用三维数组运行..你将通过写这样的方式得到答案,......

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

在数组中传递名字时请注意一点。