Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.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 如何正确迭代数组?_Php_Arrays_Json_String_Iterator - Fatal编程技术网

Php 如何正确迭代数组?

Php 如何正确迭代数组?,php,arrays,json,string,iterator,Php,Arrays,Json,String,Iterator,我有一个$content变量,echo$content显示以下内容: { "ID": 181271, "version_id": 2137, "theme_id": 2, "score": null, "showstopper": 0 } 显然echo$content['ID']应该显示181271 所以我需要迭代所有字段。我正在做: foreach ($content as $key => $value) { echo $key ;

我有一个
$content
变量,
echo$content
显示以下内容:

{
   "ID": 181271,
   "version_id": 2137,
   "theme_id": 2,
   "score": null,
   "showstopper": 0
}
显然
echo$content['ID']
应该显示
181271

所以我需要迭代所有字段。我正在做:

 foreach ($content as $key => $value) 
 {
      echo $key ;
      echo $value;
 }
它给了我一些疯狂的结果

incrementing 1 exists 1 wasRecentlyCreated timestamps 1
预期结果:

ID 181271
version_id 2137
etc...

使用此
$array=json\u decode($content,true)将json字符串转换为数组

使用此
$array=json\u decode($content,true)将json字符串转换为数组

因为它是一个json字符串,而不是数组。您必须将json_解码到一个数组中。然后您可以访问它。

因为它是一个json字符串,而不是数组。您必须将json_解码到一个数组中。然后你可以访问它

$content = '{"ID":181271,"version_id":2137,"theme_id":2, "score":null,"showstopper":0}';
$array = json_decode($content, true);
foreach ($array as $key => $value)
{
    echo $key;
    echo $value;
}