PHP字符串到JSON

时间:2012-04-12 06:12:01

标签: php json api parsing

这是我的PHP脚本

<?php

require_once 'GoogleMovieShowtimes.php';

//Requests www.google.com/movies?near=washington
$test = new GoogleMovieShowtimes('washington');
ob_start(); 
var_dump($test->parse()); 
$save = ob_get_contents(); 
ob_end_clean();  
$save=json_encode($save);
$saveJSON=json_decode($save);
print_r($saveJSON);

?>

我得到的输出就是这个

array(1)
{
    ["theater"]=> array(10)
    {
        [0]=> array(1)
        {
            ["movies"]=> array(9)
            {
                [0]=> array(2)
                {
                    ["name"]=> string(35) "The Raid: Redemption (Serbuan maut)" ["time"]=> array(8)
                    {
                        [0]=> string(59) "2:20" [1]=> string(7) "" [2]=> string(65) "  4:50" [3]=> string(13) "  " [4]=> string(65) "  7:20" [5]=> string(13) "  " [6]=> string(67) "  9:50pm" [7]=> string(13) "  "
                    }


                }

                [1]=> array(2)
                {
                    ["name"]=> string(27) "Salmon Fishing in the Yemen" ["time"]=> array(8)
                    {
                        [0]=> string(59) "1:35" [1]=> string(7) "" [2]=> string(65) "  4:15" [3]=> string(13) "  " [4]=> string(65) "  7:00" [5]=> string(13) "  " [6]=> string(67) "  9:35pm" [7]=> string(13) "  "
                    }


                }

                [2]=> array(2)
                {
                    ["name"]=> string(38) "The Kid With a Bike (Le Gamin au Velo)" ["time"]=> array(10)
                    {
                        [0]=> string(59) "1:00" [1]=> string(7) "" [2]=> string(65) "  3:15" [3]=> string(13) "  " [4]=> string(65) "  5:30" [5]=> string(13) "  " [6]=> string(65) "  7:45" [7]=> string(13) "  " [8]=> string(67) "  9:55pm" [9]=> string(13) "  "
                    }


                }

                [3]=> array(2)
                {
                    ["name"]=> string(20) "Jiro Dreams of Sushi" ["time"]=> array(10)
                    {
                        [0]=> string(59) "1:30" [1]=> string(7) "" [2]=> string(65) "  3:40" [3]=> string(13) "  " [4]=> string(65) "  5:50" [5]=> string(13) "  " [6]=> string(65) "  8:00" [7]=> string(13) "  " [8]=> string(67) "  9:55pm" [9]=> string(13) "  "
                    }


                }

                [4]=> array(2)
                {
                    ["name"]=> string(17) "The Deep Blue Sea" ["time"]=> array(2)
                    {
                        [0]=> string(61) "4:05pm" [1]=> string(7) ""
                    }


                }

                [5]=> array(2)
                {
                    ["name"]=> string(38) "A Separation (Jodaeiye Nader az Simin)" ["time"]=> array(2)
                    {
                        [0]=> string(61) "1:10pm" [1]=> string(7) ""
                    }


                }

                [6]=> array(2)
                {
                    ["name"]=> string(8) "Thin Ice" ["time"]=> array(4)
                    {
                        [0]=> string(59) "7:10" [1]=> string(7) "" [2]=> string(67) "  9:40pm" [3]=> string(13) "  "
                    }


                }

                [7]=> array(2)
                {
                    ["name"]=> string(10) "The Skinny" ["time"]=> array(10)
                    {
                        [0]=> string(59) "1:00" [1]=> string(7) "" [2]=> string(65) "  3:00" [3]=> string(13) "  " [4]=> string(65) "  5:15" [5]=> string(13) "  " [6]=> string(65) "  7:30" [7]=> string(13) "  " [8]=> string(67) "  9:45pm" [9]=> string(13) "  "
                    }


                }

                [8]=> array(2)
                {
                    ["name"]=> string(19) "Hipsters (Stilyagi)" ["time"]=> array(8)
                    {
                        [0]=> string(59) "1:15" [1]=> string(7) "" [2]=> string(65) "  4:00" [3]=> string(13) "  " [4]=> string(65) "  6:45" [5]=> string(13) "  " [6]=> string(67) "  9:30pm" [7]=> string(13) "  "
                    }


                }


            }


        }

        [1]=> array(1)
        {
            ["movies"]=> array(3)
            {
                [0]=> array(2)
                {
                    ["name"]=> string(16) "Tornado Alley 3D" ["time"]=> array(6)
                    {
                        [0]=> string(62) "11:50am" [1]=> string(7) "" [2]=> string(65) "  2:30" [3]=> string(13) "  " [4]=> string(67) "  5:10pm" [5]=> string(13) "  "
                    }


                }

                [1]=> array(2)
                {
                    ["name"]=> string(33) "Dinosaurs 3D: Giants of Patagonia" ["time"]=> array(6)
                    {
                        [0]=> string(62) "10:15am" [1]=> string(7) "" [2]=> string(66) "  12:55" [3]=> string(13) "  " [4]=> string(67) "  4:35pm" [5]=> string(13) "  "
                    }


                }

                [2]=> array(2)
                {
                    ["name"]=> string(20) "Coral Reef Adventure" ["time"]=> array(6)
                    {
                        [0]=> string(62) "10:50am" [1]=> string(7) "" [2]=> string(65) "  1:30" [3]=> string(13) "  " [4]=> string(67) "  3:35pm" [5]=> string(13) "  "
                    }


                }


            }


        }

        [2]=> array(1)
        {
            ["movies"]=> array(1)
            {
                [0]=> array(2)
                {
                    ["name"]=> string(6) "At Sea" ["time"]=> array(6)
                    {
                        [0]=> string(62) "10:00am" [1]=> string(7) "" [2]=> string(66) "  12:00" [3]=> string(13) "  " [4]=> string(67) "  2:00pm" [5]=> string(13) "  "
                    }


                }


            }


        }

        [3]=> array(1)
        {
            ["movies"]=> array(11)
            {
                [0]=> array(2)
                {
                    ["name"]=> string(16) "The Hunger Games" ["time"]=> array(24)
                    {
                        [0]=> string(294) "11:30am" [1]=> string(7) "" [2]=> string(298) "  12:10" [3]=> string(13) "  " [4]=> string(298) "  12:45" [5]=> string(13) "  " [6]=> string(297) "  3:00" [7]=> string(13) "  " [8]=> string(297) "  3:30" [9]=> string(13) "  " [10]=> string(297) "  4:10" [11]=> string(13) "  " [12]=> string(297) "  6:20" [13]=> string(13) "  " [14]=> string(297) "  6:50" [15]=> string(13) "  " [16]=> string(297) "  7:35" [17]=> string(13) "  " [18]=> string(297) "  9:35" [19]=> string(13) "  " [20]=> string(298) "  10:05" [21]=> string(13) "  " [22]=> string(300) "  10:35pm" [23]=> string(13) "  "
                    }


                }

                [1]=> array(2)
                {
                    ["name"]=> string(16) "American Reunion" ["time"]=> array(18)
                    {
                        [0]=> string(294) "11:50am" [1]=> string(7) "" [2]=> string(298) "  12:50" [3]=> string(13) "  " [4]=> string(297) "  2:40" [5]=> string(13) "  " [6]=> string(297) "  4:00" [7]=> string(13) "  " [8]=> string(297) "  5:25" [9]=> string(13) "  " [10]=> string(297) "  7:00" [11]=> string(13) "  " [12]=> string(297) "  8:00" [13]=> string(13) "  " [14]=> string(297) "  9:50" [15]=> string(13) "  " [16]=> string(300) "  10:40pm" [17]=> string(13) "  "
                    }


                }

                [2]=> array(2)
                {
                    ["name"]=> string(13) "Mirror Mirror" ["time"]=> array(8)
                    {
                        [0]=> string(291) "1:30" [1]=> string(7) "" [2]=> string(297) "  4:50" [3]=> string(13) "  " [4]=> string(297) "  7:20" [5]=> string(13) "  " [6]=> string(300) "  10:00pm" [7]=> string(13) "  "
                    }


                }

                [3]=> array(2)
                {
                    ["name"]=> string(14) "21 Jump Street" ["time"]=> array(10)
                    {
                        [0]=> string(294) "11:45am" [1]=> string(7) "" [2]=> string(297) "  2:25" [3]=> string(13) "  " [4]=> string(297) "  5:10" [5]=> string(13) "  " [6]=> string(297) "  7:50" [7]=> string(13) "  " [8]=> string(300) "  10:25pm" [9]=> string(13) "  "
                    }


                }

                [4]=> array(2)
                {
                    ["name"]=> string(10) "Titanic 3D" ["time"]=> array(12)
                    {
                        [0]=> string(292) "12:00" [1]=> string(7) "" [2]=> string(297) "  1:00" [3]=> string(13) "  " [4]=> string(297) "  4:20" [5]=> string(13) "  " [6]=> string(297) "  5:20" [7]=> string(13) "  " [8]=> string(297) "  8:30" [9]=> string(13) "  " [10]=> string(299) "  9:30pm" [11]=> string(13) "  "
                    }


                }

                [5]=> array(2)
                {
                    ["name"]=> string(22) "Wrath of the Titans 3D" ["time"]=> array(14)
                    {
                        [0]=> string(294) "11:40am" [1]=> string(7) "" [2]=> string(297) "  2:20" [3]=> string(13) "  " [4]=> string(297) "  4:15" [5]=> string(13) "  " [6]=> string(297) "  5:00" [7]=> string(13) "  " [8]=> string(297) "  7:40" [9]=> string(13) "  " [10]=> string(297) "  9:40" [11]=> string(13) "  " [12]=> string(300) "  10:20pm" [13]=> string(13) "  "
                    }


                }

                [6]=> array(2)
                {
                    ["name"]=> string(19) "Wrath of the Titans" ["time"]=> array(4)
                    {
                        [0]=> string(291) "1:10" [1]=> string(7) "" [2]=> string(299) "  7:10pm" [3]=> string(13) "  "
                    }


                }

                [7]=> array(2)
                {
                    ["name"]=> string(27) "Dr. Seuss' The Lorax 3D" ["time"]=> array(2)
                    {
                        [0]=> string(294) "12:05pm" [1]=> string(7) ""
                    }


                }

                [8]=> array(2)
                {
                    ["name"]=> string(16) "Casa de mi Padre" ["time"]=> array(2)
                    {
                        [0]=> string(293) "1:15pm" [1]=> string(7) ""
                    }


                }

                [9]=> array(2)
                {
                    ["name"]=> string(9) "Project X" ["time"]=> array(8)
                    {
                        [0]=> string(291) "3:20" [1]=> string(7) "" [2]=> string(297) "  5:40" [3]=> string(13) "  " [4]=> string(297) "  8:10" [5]=> string(13) "  " [6]=> string(300) "  10:30pm" [7]=> string(13) "  "
                    }


                }

                [10]=> array(2)
                {
                    ["name"]=> string(12) "We the Party" ["time"]=> array(4)
                    {
                        [0]=> string(292) "12:15" [1]=> string(7) "" [2]=> string(299) "  2:45pm" [3]=> string(13) "  "
                    }


                }


            }


        }

        [4]=> array(1)
        {
            ["movies"]=> array(6)
            {
                [0]=> array(2)
                {
                    ["name"]=> string(16) "Casa de mi Padre" ["time"]=> array(6)
                    {
                        [0]=> string(59) "2:40" [1]=> string(7) "" [2]=> string(65) "  7:10" [3]=> string(13) "  " [4]=> string(67) "  9:20pm" [5]=> string(13) "  "
                    }


                }

                [1]=> array(2)
                {
                    ["name"]=> string(10) "Undefeated" ["time"]=> array(6)
                    {
                        [0]=> string(59) "2:20" [1]=> string(7) "" [2]=> string(65) "  7:30" [3]=> string(13) "  " [4]=> string(67) "  9:50pm" [5]=> string(13) "  "
                    }


                }

                [2]=> array(2)
                {
                    ["name"]=> string(10) "Coriolanus" ["time"]=> array(2)
                    {
                        [0]=> string(61) "5:00pm" [1]=> string(7) ""
                    }


                }

                [3]=> array(2)
                {
                    ["name"]=> string(3) "Boy" ["time"]=> array(4)
                    {
                        [0]=> string(59) "3:30" [1]=> string(7) "" [2]=> string(67) "  6:00pm" [3]=> string(13) "  "
                    }


                }

                [4]=> array(2)
                {
                    ["name"]=> string(16) "Chico & Rita" ["time"]=> array(4)
                    {
                        [0]=> string(59) "8:00" [1]=> string(7) "" [2]=> string(68) "  10:00pm" [3]=> string(13) "  "
                    }


                }

                [5]=> array(2)
                {
                    ["name"]=> string(19) "The Long Day Closes" ["time"]=> array(2)
                    {
                        [0]=> string(61) "5:20pm" [1]=> string(7) ""
                    }


                }


            }


        }

        [5]=> array(1)
        {
            ["movies"]=> array(3)
            {
                [0]=> array(2)
                {
                    ["name"]=> string(9) "Rescue 3D" ["time"]=> array(8)
                    {
                        [0]=> string(62) "11:15am" [1]=> string(7) "" [2]=> string(66) "  12:55" [3]=> string(13) "  " [4]=> string(65) "  3:05" [5]=> string(13) "  " [6]=> string(67) "  4:50pm" [7]=> string(13) "  "
                    }


                }

                [1]=> array(2)
                {
                    ["name"]=> string(15) "IMAX: Hubble 3D" ["time"]=> array(8)
                    {
                        [0]=> string(60) "10:20" [1]=> string(7) "" [2]=> string(68) "  11:55am" [3]=> string(13) "  " [4]=> string(65) "  1:30" [5]=> string(13) "  " [6]=> string(67) "  3:45pm" [7]=> string(13) "  "
                    }


                }

                [2]=> array(2)
                {
                    ["name"]=> string(6) "To Fly" ["time"]=> array(2)
                    {
                        [0]=> string(61) "2:30pm" [1]=> string(7) ""
                    }


                }


            }


        }

        [6]=> array(1)
        {
            ["movies"]=> array(4)
            {
                [0]=> array(2)
                {
                    ["name"]=> string(20) "Journey to the Stars" ["time"]=> array(16)
                    {
                        [0]=> string(60) "12:00" [1]=> string(7) "" [2]=> string(65) "  1:00" [3]=> string(13) "  " [4]=> string(65) "  2:00" [5]=> string(13) "  " [6]=> string(65) "  3:00" [7]=> string(13) "  " [8]=> string(65) "  4:00" [9]=> string(13) "  " [10]=> string(65) "  5:00" [11]=> string(13) "  " [12]=> string(65) "  6:00" [13]=> string(13) "  " [14]=> string(67) "  7:00pm" [15]=> string(13) "  "
                    }


                }

                [1]=> array(2)
                {
                    ["name"]=> string(17) "Cosmic Collisions" ["time"]=> array(10)
                    {
                        [0]=> string(62) "11:00am" [1]=> string(7) "" [2]=> string(66) "  12:30" [3]=> string(13) "  " [4]=> string(65) "  2:30" [5]=> string(13) "  " [6]=> string(65) "  4:30" [7]=> string(13) "  " [8]=> string(67) "  6:30pm" [9]=> string(13) "  "
                    }


                }

                [2]=> array(2)
                {
                    ["name"]=> string(16) "Infinity Express" ["time"]=> array(8)
                    {
                        [0]=> string(62) "11:30am" [1]=> string(7) "" [2]=> string(65) "  1:30" [3]=> string(13) "  " [4]=> string(65) "  3:30" [5]=> string(13) "  " [6]=> string(67) "  5:30pm" [7]=> string(13) "  "
                    }


                }

                [3]=> array(2)
                {
                    ["name"]=> string(17) "The Stars Tonight" ["time"]=> array(2)
                    {
                        [0]=> string(62) "10:30am" [1]=> string(7) ""
                    }


                }


            }


        }

        [7]=> array(1)
        {
            ["movies"]=> array(1)
            {
                [0]=> array(2)
                {
                    ["name"]=> string(24) "Selection of Short Films" ["time"]=> array(4)
                    {
                        [0]=> string(60) "12:30" [1]=> string(7) "" [2]=> string(67) "  3:30pm" [3]=> string(13) "  "
                    }


                }


            }


        }

        [8]=> array(1)
        {
            ["movies"]=> array(15)
            {
                [0]=> array(2)
                {
                    ["name"]=> string(16) "The Hunger Games" ["time"]=> array(18)
                    {
                        [0]=> string(292) "12:20" [1]=> string(7) "" [2]=> string(297) "  1:20" [3]=> string(13) "  " [4]=> string(297) "  3:50" [5]=> string(13) "  " [6]=> string(297) "  4:45" [7]=> string(13) "  " [8]=> string(297) "  6:15" [9]=> string(13) "  " [10]=> string(297) "  7:15" [11]=> string(13) "  " [12]=> string(297) "  8:10" [13]=> string(13) "  " [14]=> string(297) "  9:45" [15]=> string(13) "  " [16]=> string(300) "  10:30pm" [17]=> string(13) "  "
                    }


                }

                [1]=> array(2)
                {
                    ["name"]=> string(16) "American Reunion" ["time"]=> array(16)
                    {
                        [0]=> string(292) "12:30" [1]=> string(7) "" [2]=> string(297) "  1:30" [3]=> string(13) "  " [4]=> string(297) "  3:10" [5]=> string(13) "  " [6]=> string(297) "  4:30" [7]=> string(13) "  " [8]=> string(297) "  6:00" [9]=> string(13) "  " [10]=> string(297) "  7:30" [11]=> string(13) "  " [12]=> string(297) "  9:00" [13]=> string(13) "  " [14]=> string(300) "  10:15pm" [15]=> string(13) "  "
                    }


                }

                [2]=> array(2)
                {
                    ["name"]=> string(13) "Mirror Mirror" ["time"]=> array(8)
                    {
                        [0]=> string(291) "1:40" [1]=> string(7) "" [2]=> string(297) "  4:20" [3]=> string(13) "  " [4]=> string(297) "  7:10" [5]=> string(13) "  " [6]=> string(300) "  10:00pm" [7]=> string(13) "  "
                    }


                }

                [3]=> array(2)
                {
                    ["name"]=> string(17) "The Three Stooges" ["time"]=> array(2)
                    {
                        [0]=> string(294) "12:01am" [1]=> string(7) ""
                    }


                }

                [4]=> array(2)
                {
                    ["name"]=> string(14) "21 Jump Street" ["time"]=> array(8)
                    {
                        [0]=> string(291) "2:00" [1]=> string(7) "" [2]=> string(297) "  5:10" [3]=> string(13) "  " [4]=> string(297) "  7:50" [5]=> string(13) "  " [6]=> string(300) "  10:25pm" [7]=> string(13) "  "
                    }


                }

                [5]=> array(2)
                {
                    ["name"]=> string(10) "Titanic 3D" ["time"]=> array(10)
                    {
                        [0]=> string(292) "12:00" [1]=> string(7) "" [2]=> string(297) "  2:30" [3]=> string(13) "  " [4]=> string(297) "  4:15" [5]=> string(13) "  " [6]=> string(297) "  7:00" [7]=> string(13) "  " [8]=> string(299) "  8:45pm" [9]=> string(13) "  "
                    }


                }

                [6]=> array(2)
                {
                    ["name"]=> string(22) "The Cabin in the Woods" ["time"]=> array(2)
                    {
                        [0]=> string(293) "12:01am" [1]=> string(7) ""
                    }


                }

                [7]=> array(2)
                {
                    ["name"]=> string(22) "Wrath of the Titans 3D" ["time"]=> array(8)
                    {
                        [0]=> string(292) "12:10" [1]=> string(7) "" [2]=> string(297) "  3:00" [3]=> string(13) "  " [4]=> string(297) "  5:45" [5]=> string(13) "  " [6]=> string(299) "  8:30pm" [7]=> string(13) "  "
                    }


                }

                [8]=> array(2)
                {
                    ["name"]=> string(7) "Lockout" ["time"]=> array(2)
                    {
                        [0]=> string(294) "12:01am" [1]=> string(7) ""
                    }


                }

                [9]=> array(2)
                {
                    ["name"]=> string(19) "Wrath of the Titans" ["time"]=> array(8)
                    {
                        [0]=> string(292) "12:45" [1]=> string(7) "" [2]=> string(297) "  3:30" [3]=> string(13) "  " [4]=> string(297) "  6:30" [5]=> string(13) "  " [6]=> string(299) "  9:15pm" [7]=> string(13) "  "
                    }


                }

               //removed some things to minimize word count



                [12]=> array(2)
                {
                    ["name"]=> string(27) "Salmon Fishing in the Yemen" ["time"]=> array(4)
                    {
                        [0]=> string(291) "2:45" [1]=> string(7) "" [2]=> string(299) "  8:20pm" [3]=> string(13) "  "
                    }


                }

                [13]=> array(2)
                {
                    ["name"]=> string(42) "Wrath of the Titans: An IMAX 3D Experience" ["time"]=> array(8)
                    {
                        [0]=> string(291) "2:15" [1]=> string(7) "" [2]=> string(297) "  5:00" [3]=> string(13) "  " [4]=> string(297) "  7:40" [5]=> string(13) "  " [6]=> string(300) "  10:20pm" [7]=> string(13) "  "
                    }


                }

                [14]=> array(2)
                {
                    ["name"]=> string(17) "Friends With Kids" ["time"]=> array(4)
                    {
                        [0]=> string(292) "12:05" [1]=> string(7) "" [2]=> string(299) "  5:30pm" [3]=> string(13) "  "
                    }


                }


            }


        }

        [9]=> array(1)
        {
            ["movies"]=> array(1)
            {
                [0]=> array(2)
                {
                    ["name"]=> string(16) "The Hunger Games" ["time"]=> array(6)
                    {
                        [0]=> string(292) "12:30" [1]=> string(7) "" [2]=> string(297) "  4:00" [3]=> string(13) "  " [4]=> string(299) "  7:40pm" [5]=> string(13) "  "
                    }


                }


            }


        }


    }


}

但是现在如果我做了

print_r($saveJSON["theater"]);

我得到的只是'a'......没有别的,是不是应该打印剧院阵列? 什么出错?

如果我不做json_encode()和json_decode并且只是echo $ save我得到相同的输出,那么我如何迭代这些数组呢?

1 个答案:

答案 0 :(得分:5)

  1. print_r($saveJSON["theater"]),需要print_r($saveJSON[0]["theater"]); 因为看起来saveJSON是一个包含1个元素的数组,而后者又是一个关联数组。

  2. 同时更改您的JSON解码以返回数组而不是对象。

  3. 改变这个:

    $saveJSON=json_decode($save);
    

    $saveJSON=json_decode($save,true); // to have saveJSON as an array, not object