Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:json解析中的字符串_Php_Json_String_Parsing_Foreach - Fatal编程技术网

PHP:json解析中的字符串

PHP:json解析中的字符串,php,json,string,parsing,foreach,Php,Json,String,Parsing,Foreach,我正在尝试解析json。我在foreach循环中运行此操作,如果我执行以下操作,它将正常工作: $places = array('restaurant', 'store', 'etc') foreach ($this->placesCachingTypes as $places) { $places_location_lat = $json_decoded->json[0]->restaurant[0]->geometry->location-&

我正在尝试解析json。我在foreach循环中运行此操作,如果我执行以下操作,它将正常工作:

$places = array('restaurant', 'store', 'etc')
    foreach ($this->placesCachingTypes as $places) {
      $places_location_lat = $json_decoded->json[0]->restaurant[0]->geometry->location->lat;
      $places_location_lng = $json_decoded->json[0]->restaurant[0]->geometry->location->lng;
    }
但是,当我执行以下操作时,即我将餐厅更改为$places(我需要执行此操作,因为我有一个不同的位置数组,并且我希望在foreach循环中解析所有位置),它不起作用

foreach ($this->placesCachingTypes as $places) {
  $places_location_lat = $json_decoded->json[0]->$places[0]->geometry->location->lat;
  $places_location_lng = $json_decoded->json[0]->$places[0]->geometry->location->lng;
}
解决方案正在将$places更改为{$places}[0]


$places数组包含关键字,例如餐厅或商店。所以[0]指的是json中的第一个,这就是为什么需要它。

为什么在第一个循环中有这个:

json[0]->restaurant[0]
json[0]->$restaurant[0]
但在接下来的时间里,你会:

json[0]->$places[0]
json[0]->$places[0]
可能您对JSON的解析不正确,应该是:

foreach ($this->placesCachingTypes as $places) {
  $places_location_lat = $json_decoded->json[0]->places[0]->geometry->location->lat;
  $places_location_lng = $json_decoded->json[0]->places[0]->geometry->location->lng;
}
然后在第一个循环中,您应该进行类似的编辑以删除
$restaurant[0]

foreach ($this->placesCachingTypes as $places) {
  $places_location_lat = $json_decoded->json[0]->restaurant[0]->geometry->location->lat;
  $places_location_lng = $json_decoded->json[0]->restaurant[0]->geometry->location->lng;
}

然后,当您通过
foreach($this->placesCachingTypes as$places){
进行循环时,仍然不清楚
$places
的值是什么。使用
$places
的值进行循环毫无意义。也许在
$json\u decoded->json[0]的循环对象中指定
$places
->
是您问题的根源吗?需要您提供更多信息来确认这一点。

删除美元符号?您是否尝试过
->{$places}[0]
?为什么要使用
foreach($this->placesCachingTypes as$places)循环{
$places
在第一个循环中没有真正被引用,但在第二个循环中失败时?为什么称之为“解析json”?这就是
json\u decode()
所做的。所有使用该结果的操作都只是处理数组和对象,不再是json了。Ultimater的答案成功了!