json文件上的count函数未返回正确的值(php)

json文件上的count函数未返回正确的值(php),php,json,count,Php,Json,Count,我有一个JSON文件,其中包含我想要访问的评论。看起来是这样的: { "html_attributions" : [], "result" : { "address_components" : [ { "long_name" : "Antrim Road", "short_name" :

我有一个JSON文件,其中包含我想要访问的评论。看起来是这样的:

{
   "html_attributions" : [],
   "result" : {
      "address_components" : [
         {
            "long_name" : "Antrim Road",
            "short_name" : "Antrim Rd",
            "types" : [ "route" ]
         },
         {
            "long_name" : "Belfast",
            "short_name" : "Belfast",
            "types" : [ "locality", "political" ]
         },
         {
            "long_name" : "Newtownabbey",
            "short_name" : "Newtownabbey",
            "types" : [ "postal_town" ]
         },
         {
            "long_name" : "Belfast",
            "short_name" : "Belfast",
            "types" : [ "administrative_area_level_2", "political" ]
         },
         {
            "long_name" : "Northern Ireland",
            "short_name" : "Northern Ireland",
            "types" : [ "administrative_area_level_1", "political" ]
         },
         {
            "long_name" : "United Kingdom",
            "short_name" : "GB",
            "types" : [ "country", "political" ]
         },
         {
            "long_name" : "BT36 7PN",
            "short_name" : "BT36 7PN",
            "types" : [ "postal_code" ]
         }
      ],
      "adr_address" : "\u003cspan class=\"street-address\"\u003eAntrim Road\u003c/span\u003e, \u003cspan class=\"extended-address\"\u003eBelfast\u003c/span\u003e, \u003cspan class=\"locality\"\u003eNewtownabbey\u003c/span\u003e \u003cspan class=\"postal-code\"\u003eBT36 7PN\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eUK\u003c/span\u003e",
      "business_status" : "OPERATIONAL",
      "formatted_address" : "Antrim Rd, Belfast, Newtownabbey BT36 7PN, UK",
      "formatted_phone_number" : "028 9077 6277",
      "geometry" : {
         "location" : {
            "lat" : 54.6577821,
            "lng" : -5.943665699999999
         },
         "viewport" : {
            "northeast" : {
               "lat" : 54.65913108029149,
               "lng" : -5.942316719708497
            },
            "southwest" : {
               "lat" : 54.6564331197085,
               "lng" : -5.945014680291502
            }
         }
      },
      "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/generic_business-71.png",
      "international_phone_number" : "+44 28 9077 6277",
      "name" : "Belfast Zoo",
      "photos" : [
         {
            "height" : 1243,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/111863060002111236588\"\u003eBelfast Zoo\u003c/a\u003e"
            ],
            "photo_reference" : "ATtYBwJTIiPI3VznHujO9vTlzCagiH0Ak6dcoeZZsuNi8aIR1HzkLKmDbAEq-8ctO68YLbIuAqJeiu9X1tyhC6mKjcns0wGPRtTDqbhFTyle7LPE2oRa5Oda2FXzSDf8nrC1WR44YkdyPUhZUnN431HztHIFkbJF_eEYGvQffmflID9TqALU",
            "width" : 1776
         },
         {
            "height" : 2988,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/100936618118839321951\"\u003eAmanda Lewis\u003c/a\u003e"
            ],
            "photo_reference" : "ATtYBwI55L16mowjExP5cOFV2wKThreGKXYT_1t6jK4JkGcUap94VNPF9dN2OniYKmi9YGDNqALwY4-6MWZoxm6-zyDNxog8g7c8UwD9UpXVrivmkfCpuY9MguXFiJlRVw9noyV6QMI-TO2bEVbvd7wQgnXn1Eaig_puaCDcylTcZ8nrOJAp",
            "width" : 5312
         },
         {
            "height" : 2988,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/116390912159770135890\"\u003eBeti Na\u003c/a\u003e"
            ],
            "photo_reference" : "ATtYBwJQ94yxuoAhN3MTMbf7DOdTgpJAYUoWbHFQW_i_uh6eQqdSdWzAiy5Du48Lm3bDnpWSPI4daH3AHem_XweVtP4MSaIPQZe4ozYWbEX1RPrKtDRMGu3kmqsMSCS84kyMLTlDOYpGuC7w4gqEzaqcg5DbIXiPoIAjXzFxTPCV1OdeRm8Q",
            "width" : 5312
         },
         {
            "height" : 9248,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/106644795451552578979\"\u003eMatthew mcmullan\u003c/a\u003e"
            ],
            "photo_reference" : "ATtYBwJDE3wE13EJptVWwrlPe_-n9HnwqU8Hs5y7HpWm_5hqMXvmo3AaZzyZsoZTfyV1M69dFgZnsJrP9iyiarwEuXYHPOA4ogvr0j-pn5XPk4nBdVrnPtbgPh0JYoym62ac3y1I33XBvwc-1eXk_MJbQJ_UvIcr2c6OmDLZSq0r4ESN6y7J",
            "width" : 6936
         },
         {
            "height" : 2988,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/116390912159770135890\"\u003eBeti Na\u003c/a\u003e"
            ],
            "photo_reference" : "ATtYBwJbAFdJSWGNrd6uJb6yf_oM4Rfn2fkbewqz2lHTWl0buXcQ78YbBiJ8Ta_umMEo3XrFOS599cO-x01OvS83tiiIdlRXu8NOWcLxSpZmYuq41kG2KVyWz1MOJ9YjqZbLdIzADlZ641EipOAcmmZv-TBdbzHwHC7S3wrYrdaa67p7rvTR",
            "width" : 5312
         },
         {
            "height" : 3823,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/116284097622909280304\"\u003eBrian Cunningham\u003c/a\u003e"
            ],
            "photo_reference" : "ATtYBwL7Z-RSxZ6pZ-i4XmKKgGNCazKNVw2bITY5sN1FFlDpbF01rF21rlpi54WJCHf23se8yAertLRQb-kAq2PLYB9ymsiQnrveFL-GttZjyGNwvuXpos-g9Lm0J58_I32Mr6YcgS5wfJIp74DQv2PyE6Wjo8NUrVyj5sLctb1FpVS92PuJ",
            "width" : 5735
         },
         {
            "height" : 4000,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/109005734355336105418\"\u003eWilliam Young\u003c/a\u003e"
            ],
            "photo_reference" : "ATtYBwKPd5mh06H7KLngtPKQPfU8Wfq2BD08-iL51gJqhZea1zrZAK-eT6fAM3X8qde9TkciaMCgYo64_Ik1MEIIBAzDBYOt_2ug0pkeg3BWM7TNKyFu6k__qcImA2CFJE1AMsiKitlonSApY4w1t53D75rO0xEpmmL8cMRSIEt7YNrGhVSb",
            "width" : 2723
         },
         {
            "height" : 3024,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/100719597211687837516\"\u003eJonathan Kelly\u003c/a\u003e"
            ],
            "photo_reference" : "ATtYBwJCyTy5R0N7jNCmnggCXgn70Xgnws1KBXocRpAfjKBjEVZszRMQpQ9-ZZxZcPdaWzDKDtUhnEHyavQio3u3etvoc5q6RFaLGhOA7SK58wE84pbMEz5YazqArwuEA0jDBNEctRMO9J9DTYDhkt4jfrbTAke1-AfJTZFkukLwXjpA_oEA",
            "width" : 4032
         },
         {
            "height" : 3024,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/104554106193648036788\"\u003eAjay Jose\u003c/a\u003e"
            ],
            "photo_reference" : "ATtYBwKrV-cN2oWcr0iD65VKbfYTQLy1vLDPVs2YnbEtFq3Ne5KH16ooiWCD18Nc6VjZZ1Jr5Lt54yQtbRN7XIr-78e6YwtXrV1ue_0aHahqpv17YfscGFzfQ7X5SW0gEU19dbVSX0oC3BGdzAbHmjO04OXNS2vu6u1HDKT67mWlRTkSYovM",
            "width" : 4032
         },
         {
            "height" : 3024,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/100719597211687837516\"\u003eJonathan Kelly\u003c/a\u003e"
            ],
            "photo_reference" : "ATtYBwKgM41e8PE5cKx_nAgU_fW4XHJeIy-StMar51N7ZbVjByUpfiqS7FbtQ-gWkGrRt9vNqOcsb9BysvgErNi-Ai0TEcQ2pLv5qOuyQ1S2uBWTWEksuV6wzDcXYNKTLa7uE-_H_rBH2pKoKCWl0meTtuq55HuNuFOkN6OEQESk-DN4ZzQH",
            "width" : 4032
         }
      ],
      "place_id" : "ChIJ88iCe5inYUgRdeXmfCtuvu4",
      "plus_code" : {
         "compound_code" : "M354+4G Belfast, UK",
         "global_code" : "9C6PM354+4G"
      },
      "rating" : 4.3,
      "reference" : "ChIJ88iCe5inYUgRdeXmfCtuvu4",
      "reviews" : [
         {
            "author_name" : "Karen Hoad",
            "author_url" : "https://www.google.com/maps/contrib/103129952317107981705/reviews",
            "language" : "en",
            "profile_photo_url" : "https://lh6.googleusercontent.com/-OkGM8wAyHz8/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucn55YdMQZ1BX6ods-S34cieMQODrg/s128-c0x00000000-cc-rp-mo-ba5/photo.jpg",
            "rating" : 4,
            "relative_time_description" : "6 months ago",
            "text" : "Great place just not good when raining as the animals stay in",
            "time" : 1599236033
         },
         {
            "author_name" : "John Lee Ashworth",
            "author_url" : "https://www.google.com/maps/contrib/116452096322094109908/reviews",
            "language" : "en",
            "profile_photo_url" : "https://lh3.googleusercontent.com/-_ZpK7cdK1NI/AAAAAAAAAAI/AAAAAAAAAAA/AMZuuclmcdFSQNilc5MfhJ3m8rRqzAXO_A/s128-c0x00000000-cc-rp-mo/photo.jpg",
            "rating" : 3,
            "relative_time_description" : "6 months ago",
            "text" : "Love zoos, love animals, hate nothing but steep hills though",
            "time" : 1598880406
         },
         {
            "author_name" : "Clare",
            "author_url" : "https://www.google.com/maps/contrib/116989491220973031798/reviews",
            "language" : "en",
            "profile_photo_url" : "https://lh3.googleusercontent.com/a-/AOh14GjK9VEovk8XRG41K9vVtu0SKRIHQdXp6Gb79opr_g=s128-c0x00000000-cc-rp-mo-ba2",
            "rating" : 2,
            "relative_time_description" : "5 months ago",
            "text" : "Very little animals to see.",
            "time" : 1600628749
         },
         {
            "author_name" : "Tammy 102",
            "author_url" : "https://www.google.com/maps/contrib/111672456708879530776/reviews",
            "language" : "en",
            "profile_photo_url" : "https://lh5.googleusercontent.com/-ngjoEXxeE8Q/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucnTOkOTeq8ecWLEVbSs7UBnsUPmSQ/s128-c0x00000000-cc-rp-mo-ba3/photo.jpg",
            "rating" : 5,
            "relative_time_description" : "6 months ago",
            "text" : "To check prices",
            "time" : 1600610117
         },
         {
            "author_name" : "Mark Woonton",
            "author_url" : "https://www.google.com/maps/contrib/106800516570033737478/reviews",
            "language" : "en",
            "profile_photo_url" : "https://lh3.googleusercontent.com/a-/AOh14GgnYZybrP-MTC4U45agdIlXMIFDIuH1v6roRuJAWQ=s128-c0x00000000-cc-rp-mo",
            "rating" : 5,
            "relative_time_description" : "6 months ago",
            "text" : "Amazing zoo, so much to see, a great day out",
            "time" : 1599993064
         }
      ],
      "types" : [ "zoo", "tourist_attraction", "point_of_interest", "establishment" ],
      "url" : "https://maps.google.com/?cid=17203308759660029301",
      "user_ratings_total" : 2714,
      "utc_offset" : 0,
      "vicinity" : "Antrim Road, Belfast",
      "website" : "http://www.belfastzoo.co.uk/"
   },
   "status" : "OK"
}
$objFromJson = json_decode($jsonString, true);
$reviews=$objFromJson["result"]["reviews"];
$reviewCount = count($reviews); // 5

echo "The count is $reviewCount, here are the individual reviews:\n\n";
foreach ($reviews as $rev) echo $rev["author_name"].": ".$rev["text"]."\n";
它仍在继续,但我想循环浏览评论的数量,这样我就可以抓住所有评论并将其发布到我的网站上

我得到的计数如下:

$json = json_decode($place_id, true);
$reviewCount = count($json);
然后我有我的for循环:

for ($i = 0; $i < $reviewCount; $i++){
...
}
for($i=0;$i<$reviewCount;$i++){
...
}
我意识到我的计数总是返回3。我使用var_dump来查看它的实际价值,而不是评论

我不知道如何计算评论的数量,所以我被困在这一点上

任何帮助,甚至信息都会被大量地感谢,谢谢


(经过编辑以提供整个json文件)

好的,看到您更新了问题,您需要像这样计算您的评论:

{
   "html_attributions" : [],
   "result" : {
      "address_components" : [
         {
            "long_name" : "Antrim Road",
            "short_name" : "Antrim Rd",
            "types" : [ "route" ]
         },
         {
            "long_name" : "Belfast",
            "short_name" : "Belfast",
            "types" : [ "locality", "political" ]
         },
         {
            "long_name" : "Newtownabbey",
            "short_name" : "Newtownabbey",
            "types" : [ "postal_town" ]
         },
         {
            "long_name" : "Belfast",
            "short_name" : "Belfast",
            "types" : [ "administrative_area_level_2", "political" ]
         },
         {
            "long_name" : "Northern Ireland",
            "short_name" : "Northern Ireland",
            "types" : [ "administrative_area_level_1", "political" ]
         },
         {
            "long_name" : "United Kingdom",
            "short_name" : "GB",
            "types" : [ "country", "political" ]
         },
         {
            "long_name" : "BT36 7PN",
            "short_name" : "BT36 7PN",
            "types" : [ "postal_code" ]
         }
      ],
      "adr_address" : "\u003cspan class=\"street-address\"\u003eAntrim Road\u003c/span\u003e, \u003cspan class=\"extended-address\"\u003eBelfast\u003c/span\u003e, \u003cspan class=\"locality\"\u003eNewtownabbey\u003c/span\u003e \u003cspan class=\"postal-code\"\u003eBT36 7PN\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eUK\u003c/span\u003e",
      "business_status" : "OPERATIONAL",
      "formatted_address" : "Antrim Rd, Belfast, Newtownabbey BT36 7PN, UK",
      "formatted_phone_number" : "028 9077 6277",
      "geometry" : {
         "location" : {
            "lat" : 54.6577821,
            "lng" : -5.943665699999999
         },
         "viewport" : {
            "northeast" : {
               "lat" : 54.65913108029149,
               "lng" : -5.942316719708497
            },
            "southwest" : {
               "lat" : 54.6564331197085,
               "lng" : -5.945014680291502
            }
         }
      },
      "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/generic_business-71.png",
      "international_phone_number" : "+44 28 9077 6277",
      "name" : "Belfast Zoo",
      "photos" : [
         {
            "height" : 1243,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/111863060002111236588\"\u003eBelfast Zoo\u003c/a\u003e"
            ],
            "photo_reference" : "ATtYBwJTIiPI3VznHujO9vTlzCagiH0Ak6dcoeZZsuNi8aIR1HzkLKmDbAEq-8ctO68YLbIuAqJeiu9X1tyhC6mKjcns0wGPRtTDqbhFTyle7LPE2oRa5Oda2FXzSDf8nrC1WR44YkdyPUhZUnN431HztHIFkbJF_eEYGvQffmflID9TqALU",
            "width" : 1776
         },
         {
            "height" : 2988,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/100936618118839321951\"\u003eAmanda Lewis\u003c/a\u003e"
            ],
            "photo_reference" : "ATtYBwI55L16mowjExP5cOFV2wKThreGKXYT_1t6jK4JkGcUap94VNPF9dN2OniYKmi9YGDNqALwY4-6MWZoxm6-zyDNxog8g7c8UwD9UpXVrivmkfCpuY9MguXFiJlRVw9noyV6QMI-TO2bEVbvd7wQgnXn1Eaig_puaCDcylTcZ8nrOJAp",
            "width" : 5312
         },
         {
            "height" : 2988,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/116390912159770135890\"\u003eBeti Na\u003c/a\u003e"
            ],
            "photo_reference" : "ATtYBwJQ94yxuoAhN3MTMbf7DOdTgpJAYUoWbHFQW_i_uh6eQqdSdWzAiy5Du48Lm3bDnpWSPI4daH3AHem_XweVtP4MSaIPQZe4ozYWbEX1RPrKtDRMGu3kmqsMSCS84kyMLTlDOYpGuC7w4gqEzaqcg5DbIXiPoIAjXzFxTPCV1OdeRm8Q",
            "width" : 5312
         },
         {
            "height" : 9248,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/106644795451552578979\"\u003eMatthew mcmullan\u003c/a\u003e"
            ],
            "photo_reference" : "ATtYBwJDE3wE13EJptVWwrlPe_-n9HnwqU8Hs5y7HpWm_5hqMXvmo3AaZzyZsoZTfyV1M69dFgZnsJrP9iyiarwEuXYHPOA4ogvr0j-pn5XPk4nBdVrnPtbgPh0JYoym62ac3y1I33XBvwc-1eXk_MJbQJ_UvIcr2c6OmDLZSq0r4ESN6y7J",
            "width" : 6936
         },
         {
            "height" : 2988,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/116390912159770135890\"\u003eBeti Na\u003c/a\u003e"
            ],
            "photo_reference" : "ATtYBwJbAFdJSWGNrd6uJb6yf_oM4Rfn2fkbewqz2lHTWl0buXcQ78YbBiJ8Ta_umMEo3XrFOS599cO-x01OvS83tiiIdlRXu8NOWcLxSpZmYuq41kG2KVyWz1MOJ9YjqZbLdIzADlZ641EipOAcmmZv-TBdbzHwHC7S3wrYrdaa67p7rvTR",
            "width" : 5312
         },
         {
            "height" : 3823,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/116284097622909280304\"\u003eBrian Cunningham\u003c/a\u003e"
            ],
            "photo_reference" : "ATtYBwL7Z-RSxZ6pZ-i4XmKKgGNCazKNVw2bITY5sN1FFlDpbF01rF21rlpi54WJCHf23se8yAertLRQb-kAq2PLYB9ymsiQnrveFL-GttZjyGNwvuXpos-g9Lm0J58_I32Mr6YcgS5wfJIp74DQv2PyE6Wjo8NUrVyj5sLctb1FpVS92PuJ",
            "width" : 5735
         },
         {
            "height" : 4000,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/109005734355336105418\"\u003eWilliam Young\u003c/a\u003e"
            ],
            "photo_reference" : "ATtYBwKPd5mh06H7KLngtPKQPfU8Wfq2BD08-iL51gJqhZea1zrZAK-eT6fAM3X8qde9TkciaMCgYo64_Ik1MEIIBAzDBYOt_2ug0pkeg3BWM7TNKyFu6k__qcImA2CFJE1AMsiKitlonSApY4w1t53D75rO0xEpmmL8cMRSIEt7YNrGhVSb",
            "width" : 2723
         },
         {
            "height" : 3024,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/100719597211687837516\"\u003eJonathan Kelly\u003c/a\u003e"
            ],
            "photo_reference" : "ATtYBwJCyTy5R0N7jNCmnggCXgn70Xgnws1KBXocRpAfjKBjEVZszRMQpQ9-ZZxZcPdaWzDKDtUhnEHyavQio3u3etvoc5q6RFaLGhOA7SK58wE84pbMEz5YazqArwuEA0jDBNEctRMO9J9DTYDhkt4jfrbTAke1-AfJTZFkukLwXjpA_oEA",
            "width" : 4032
         },
         {
            "height" : 3024,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/104554106193648036788\"\u003eAjay Jose\u003c/a\u003e"
            ],
            "photo_reference" : "ATtYBwKrV-cN2oWcr0iD65VKbfYTQLy1vLDPVs2YnbEtFq3Ne5KH16ooiWCD18Nc6VjZZ1Jr5Lt54yQtbRN7XIr-78e6YwtXrV1ue_0aHahqpv17YfscGFzfQ7X5SW0gEU19dbVSX0oC3BGdzAbHmjO04OXNS2vu6u1HDKT67mWlRTkSYovM",
            "width" : 4032
         },
         {
            "height" : 3024,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/100719597211687837516\"\u003eJonathan Kelly\u003c/a\u003e"
            ],
            "photo_reference" : "ATtYBwKgM41e8PE5cKx_nAgU_fW4XHJeIy-StMar51N7ZbVjByUpfiqS7FbtQ-gWkGrRt9vNqOcsb9BysvgErNi-Ai0TEcQ2pLv5qOuyQ1S2uBWTWEksuV6wzDcXYNKTLa7uE-_H_rBH2pKoKCWl0meTtuq55HuNuFOkN6OEQESk-DN4ZzQH",
            "width" : 4032
         }
      ],
      "place_id" : "ChIJ88iCe5inYUgRdeXmfCtuvu4",
      "plus_code" : {
         "compound_code" : "M354+4G Belfast, UK",
         "global_code" : "9C6PM354+4G"
      },
      "rating" : 4.3,
      "reference" : "ChIJ88iCe5inYUgRdeXmfCtuvu4",
      "reviews" : [
         {
            "author_name" : "Karen Hoad",
            "author_url" : "https://www.google.com/maps/contrib/103129952317107981705/reviews",
            "language" : "en",
            "profile_photo_url" : "https://lh6.googleusercontent.com/-OkGM8wAyHz8/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucn55YdMQZ1BX6ods-S34cieMQODrg/s128-c0x00000000-cc-rp-mo-ba5/photo.jpg",
            "rating" : 4,
            "relative_time_description" : "6 months ago",
            "text" : "Great place just not good when raining as the animals stay in",
            "time" : 1599236033
         },
         {
            "author_name" : "John Lee Ashworth",
            "author_url" : "https://www.google.com/maps/contrib/116452096322094109908/reviews",
            "language" : "en",
            "profile_photo_url" : "https://lh3.googleusercontent.com/-_ZpK7cdK1NI/AAAAAAAAAAI/AAAAAAAAAAA/AMZuuclmcdFSQNilc5MfhJ3m8rRqzAXO_A/s128-c0x00000000-cc-rp-mo/photo.jpg",
            "rating" : 3,
            "relative_time_description" : "6 months ago",
            "text" : "Love zoos, love animals, hate nothing but steep hills though",
            "time" : 1598880406
         },
         {
            "author_name" : "Clare",
            "author_url" : "https://www.google.com/maps/contrib/116989491220973031798/reviews",
            "language" : "en",
            "profile_photo_url" : "https://lh3.googleusercontent.com/a-/AOh14GjK9VEovk8XRG41K9vVtu0SKRIHQdXp6Gb79opr_g=s128-c0x00000000-cc-rp-mo-ba2",
            "rating" : 2,
            "relative_time_description" : "5 months ago",
            "text" : "Very little animals to see.",
            "time" : 1600628749
         },
         {
            "author_name" : "Tammy 102",
            "author_url" : "https://www.google.com/maps/contrib/111672456708879530776/reviews",
            "language" : "en",
            "profile_photo_url" : "https://lh5.googleusercontent.com/-ngjoEXxeE8Q/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucnTOkOTeq8ecWLEVbSs7UBnsUPmSQ/s128-c0x00000000-cc-rp-mo-ba3/photo.jpg",
            "rating" : 5,
            "relative_time_description" : "6 months ago",
            "text" : "To check prices",
            "time" : 1600610117
         },
         {
            "author_name" : "Mark Woonton",
            "author_url" : "https://www.google.com/maps/contrib/106800516570033737478/reviews",
            "language" : "en",
            "profile_photo_url" : "https://lh3.googleusercontent.com/a-/AOh14GgnYZybrP-MTC4U45agdIlXMIFDIuH1v6roRuJAWQ=s128-c0x00000000-cc-rp-mo",
            "rating" : 5,
            "relative_time_description" : "6 months ago",
            "text" : "Amazing zoo, so much to see, a great day out",
            "time" : 1599993064
         }
      ],
      "types" : [ "zoo", "tourist_attraction", "point_of_interest", "establishment" ],
      "url" : "https://maps.google.com/?cid=17203308759660029301",
      "user_ratings_total" : 2714,
      "utc_offset" : 0,
      "vicinity" : "Antrim Road, Belfast",
      "website" : "http://www.belfastzoo.co.uk/"
   },
   "status" : "OK"
}
$objFromJson = json_decode($jsonString, true);
$reviews=$objFromJson["result"]["reviews"];
$reviewCount = count($reviews); // 5

echo "The count is $reviewCount, here are the individual reviews:\n\n";
foreach ($reviews as $rev) echo $rev["author_name"].": ".$rev["text"]."\n";
我还将您的
for
循环更改为
foreach
循环,请参见我的(更新)演示:

输出:

The count is 5, here are the individual reviews:

Karen Hoad: Great place just not good when raining as the animals stay in
John Lee Ashworth: Love zoos, love animals, hate nothing but steep hills though
Clare: Very little animals to see.
Tammy 102: To check prices
Mark Woonton: Amazing zoo, so much to see, a great day out

当然你想要
count($json['reviews'])
这可能是因为解码的
json
对象只有3个键,即,
评级
参考
评论
。。。所以它的长度是3。您需要计算
评论的长度
位。类似于
count($json->reviews)
的内容放在一边:在这里显示的代码中,您将计数分配给
reviewCount
(前面需要一个
$
,因为它是一个PHP变量),但在循环中您将与
$elementCount
进行比较。。。我假设这只是问题中的一个错误,而不是实际代码中的一个错误?是的,这只是问题中的一个错误,但很好!我尝试了NigelRen和@CD001给出的方法,但出现了以下错误:“警告:尝试读取C:\xampp\htdocs\cw\tester.php第75行中数组的属性“reviews”。“问题是它返回以下内容:“致命错误:未捕获的TypeError:count():参数#1($var)必须是Countable | array类型,在C:\xampp\htdocs\cw\tester.php:44堆栈跟踪:#0{main}中提供null。它在rextester代码段中确实有效。也许你的PHP文件本身还没有正确设置其他内容?好吧,我想我明白是什么错了,这是我的学生错误。。我会更新这个问题,但我会给出整个JSON文件,“reviews”在对象“result”中,如果你能帮助我获得完整的JSON,那将是令人惊讶的!