将JSON数据显示到数组

时间:2011-10-04 07:57:41

标签: php json

我正在尝试将我的JSON数据转换为数组&然后显示来自tje JSON数据的记录计数。

我已经解码了JSON数据,但我无法计算记录。数据:

Array
(
    [http://apps.facebook.com/moviecornpopcorn/index.php/ratemovie/comments?movieid=513] => Array
        (
            [data] => Array
                (
                    [0] => Array
                        (
                            [id] => 10150301107681248_18650149
                            [from] => Array
                                (
                                    [name] => Padma Priya
                                    [id] => 100002640672777
                                )

                            [message] => nice
                            [created_time] => 2011-09-12T13:43:39+0000
                        )

                    [1] => Array
                        (
                            [id] => 10150301107681248_18687009
                            [from] => Array
                                (
                                    [name] => Rozyfor Kris
                                    [id] => 100000565855467
                                )

                            [message] => good movie
                            [created_time] => 2011-09-14T10:22:01+0000
                            [comments] => Array
                                (
                                    [data] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [id] => 10150312170016248
                                                    [from] => Array
                                                        (
                                                            [name] => Rozyfor Kris
                                                            [id] => 100000565855467
                                                        )

                                                    [message] => yes
                                                    [created_time] => 2011-09-14T12:17:47+0000
                                                )

                                            [1] => Array
                                                (
                                                    [id] => 10150312174296248
                                                    [from] => Array
                                                        (
                                                            [name] => Padma Priya
                                                            [id] => 100002640672777
                                                        )

                                                    [message] => Rozyfor Kris  yes
                                                    [created_time] => 2011-09-14T12:22:56+0000
                                                )

                                        )

                                    [count] => 2
                                )

                            [likes] => 1
                        )

                    [2] => Array
                        (
                            [id] => 10150301107681248_18687545
                            [from] => Array
                                (
                                    [name] => Padma Priya
                                    [id] => 100002640672777
                                )

                            [message] => tests
                            [created_time] => 2011-09-14T11:13:30+0000
                        )

                    [3] => Array
                        (
                            [id] => 10150301107681248_18688752
                            [from] => Array
                                (
                                    [name] => Sravanthi Pasaragonda
                                    [id] => 100001798938835
                                )

                            [message] => good moviegood movie.
                            [created_time] => 2011-09-14T12:56:24+0000
                        )

                    [4] => Array
                        (
                            [id] => 10150301107681248_18705933
                            [from] => Array
                                (
                                    [name] => Sravanthi Pasaragonda
                                    [id] => 100001798938835
                                )

                            [message] => kagal looking cool.
                            [created_time] => 2011-09-15T06:10:33+0000
                        )

                    [5] => Array
                        (
                            [id] => 10150301107681248_18707928
                            [from] => Array
                                (
                                    [name] => Sushma Murali
                                    [id] => 100002207093918
                                )

                            [message] => test
                            [created_time] => 2011-09-15T10:23:00+0000
                        )

                    [6] => Array
                        (
                            [id] => 10150301107681248_18856609
                            [from] => Array
                                (
                                    [name] => Padma Priya
                                    [id] => 100002640672777
                                )

                            [message] => NIce movie
                            [created_time] => 2011-09-23T13:42:42+0000
                            [likes] => 1
                        )

                    [7] => Array
                        (
                            [id] => 10150301107681248_18856718
                            [from] => Array
                                (
                                    [name] => Padma Priya
                                    [id] => 100002640672777
                                )

                            [message] => good
                            [created_time] => 2011-09-23T13:50:36+0000
                        )

                    [8] => Array
                        (
                            [id] => 10150301107681248_18939810
                            [from] => Array
                                (
                                    [name] => Padma Priya
                                    [id] => 100002640672777
                                )

                            [message] => wowww
                            [created_time] => 2011-09-28T08:15:06+0000
                        )

                    [9] => Array
                        (
                            [id] => 10150301107681248_18957751
                            [from] => Array
                                (
                                    [name] => Padma Priya
                                    [id] => 100002640672777
                                )

                            [message] => hiiii
                            [created_time] => 2011-09-29T05:49:35+0000
                        )

                    [10] => Array
                        (
                            [id] => 10150301107681248_18990995
                            [from] => Array
                                (
                                    [name] => Padma Priya
                                    [id] => 100002640672777
                                )

                            [message] => hello
                            [created_time] => 2011-10-01T04:37:22+0000
                        )

                    [11] => Array
                        (
                            [id] => 10150301107681248_19022504
                            [from] => Array
                                (
                                    [name] => Padma Priya
                                    [id] => 100002640672777
                                )

                            [message] => testttt
                            [created_time] => 2011-10-03T08:12:04+0000
                        )

                )

        )

)

2 个答案:

答案 0 :(得分:1)

您可以使用count函数获取任何数组的计数。

您可以使用它的数组键访问任何子数组。

$records = $array['http://apps.facebook.com/moviecornpopcorn/index.php/ratemovie/comments?movieid=513']['data'];
$count = count($records);

请参阅:Count elements in adjacent array

答案 1 :(得分:0)

你有非常讨厌的数组键:)所以获取当前元素(使用url作为数组键)值并计算它的值

<?php $data = current($yourarray); $dataCount = count($data); ?>