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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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_Loops_Foreach_Json - Fatal编程技术网

Php 在JSON字符串中循环

Php 在JSON字符串中循环,php,loops,foreach,json,Php,Loops,Foreach,Json,我试图使用foreach()循环遍历JSON字符串。但是,我不断得到以下错误: "Notice: Trying to get property of non-object". $info = json_decode($test, true); function tagGen($info){ foreach($info as $key => $value){ } 奇怪的是,当我复制并粘贴JSON字符串,然后运行foreach()时,它工作得很好。为了提供一些细节,我使用的是百思

我试图使用
foreach()
循环遍历JSON字符串。但是,我不断得到以下错误:

"Notice: Trying to get property of non-object". 
$info = json_decode($test, true); 
function tagGen($info){
foreach($info as $key => $value){
 }  
奇怪的是,当我复制并粘贴JSON字符串,然后运行
foreach()
时,它工作得很好。为了提供一些细节,我使用的是百思买API

$info = json_decode($test, true); 
function tagGen($info){
foreach($info as $key => $value){
 }  
既然这似乎对每个人都适用,那么百思买提供给我的数据是否可能有问题

$info = json_decode($test, true); 
function tagGen($info){
foreach($info as $key => $value){
 }  
请帮忙,我什么都试过了

$info = json_decode($test, true); 
function tagGen($info){
foreach($info as $key => $value){
 }  
更新很抱歉没有发布代码。这是:

$info = json_decode($test, true); 
function tagGen($info){
foreach($info as $key => $value){
 }  

您可能没有在JSON字符串上使用。看看我是如何做到这一点的:

$info = json_decode($test, true); 
function tagGen($info){
foreach($info as $key => $value){
 }  
$json = "somejsonstring";
$json_array = json_decode($json, true);

foreach($json_array as $element) {
   echo $element['some key'];
}
注意json_decode方法的第二个参数“true”。它返回一个关联数组,而不是标准的PHP对象。使在foreach循环中使用变得更加容易

$info = json_decode($test, true); 
function tagGen($info){
foreach($info as $key => $value){
 }  

希望这会有所帮助,尽管您的问题应该包含一个代码示例。

您可能没有在JSON字符串上使用。看看我是如何做到这一点的:

$info = json_decode($test, true); 
function tagGen($info){
foreach($info as $key => $value){
 }  
$json = "somejsonstring";
$json_array = json_decode($json, true);

foreach($json_array as $element) {
   echo $element['some key'];
}
注意json_decode方法的第二个参数“true”。它返回一个关联数组,而不是标准的PHP对象。使在foreach循环中使用变得更加容易

$info = json_decode($test, true); 
function tagGen($info){
foreach($info as $key => $value){
 }  

希望这能有所帮助,尽管你的问题应该包括一个代码示例。

因为你没有发布代码,所以我们可以认为你有一个字符串。我亲爱的json编码字符串是某种javascript形式的东西。foreach是一个php循环。所以,若您有一个json编码的字符串,并且希望在foreach循环中使用它,那个么您必须使用函数来实现它

$info = json_decode($test, true); 
function tagGen($info){
foreach($info as $key => $value){
 }  
当您应用json_decode时,您将从

$info = json_decode($test, true); 
function tagGen($info){
foreach($info as $key => $value){
 }  
返回用json编码的适当PHP类型的值

$info = json_decode($test, true); 
function tagGen($info){
foreach($info as $key => $value){
 }  
值true、false和null(不区分大小写)分别作为true、false和null返回

$info = json_decode($test, true); 
function tagGen($info){
foreach($info as $key => $value){
 }  
如果无法解码json或编码数据深度超过递归限制,则返回NULL

$info = json_decode($test, true); 
function tagGen($info){
foreach($info as $key => $value){
 }  
对于一些示例代码

$info = json_decode($test, true); 
function tagGen($info){
foreach($info as $key => $value){
 }  
$str=json_decode($yourjson);
foreach($str as $key=>$value)
{}

因为您没有发布代码,所以我们可以认为您已经对字符串进行了编码。我亲爱的json编码字符串是某种javascript形式的东西。foreach是一个php循环。所以,若您有一个json编码的字符串,并且希望在foreach循环中使用它,那个么您必须使用函数来实现它

$info = json_decode($test, true); 
function tagGen($info){
foreach($info as $key => $value){
 }  
当您应用json_decode时,您将从

$info = json_decode($test, true); 
function tagGen($info){
foreach($info as $key => $value){
 }  
返回用json编码的适当PHP类型的值

$info = json_decode($test, true); 
function tagGen($info){
foreach($info as $key => $value){
 }  
值true、false和null(不区分大小写)分别作为true、false和null返回

$info = json_decode($test, true); 
function tagGen($info){
foreach($info as $key => $value){
 }  
如果无法解码json或编码数据深度超过递归限制,则返回NULL

$info = json_decode($test, true); 
function tagGen($info){
foreach($info as $key => $value){
 }  
对于一些示例代码

$info = json_decode($test, true); 
function tagGen($info){
foreach($info as $key => $value){
 }  
$str=json_decode($yourjson);
foreach($str as $key=>$value)
{}

将json字符串打印到可以复制的地方,然后使用此工具检查它是否有效:

将json字符串打印到可以复制的地方,然后使用此工具检查它是否有效:

代码示例将非常有用,因为我们不知道您是否有打字错误或更深层次的问题。无论如何,我会尝试一个答案……一个代码示例将非常有用,因为我们不知道您是否有打字错误或更深层次的问题。不管怎样,我会尝试一个答案…谢谢你的快速回复。我使用的是json_解码,这里它直接来自程序。我在扯头发$test=curl\u exec($curl\u object);curl\u close($curl\u object);$info=json_decode($test,true);谢谢你的快速回复。我使用的是json_解码,这里它直接来自程序。我在扯头发$test=curl\u exec($curl\u object);curl\u close($curl\u object);$info=json_decode($test,true);似乎只要Json在一个变量中,我就会得到这个错误。当我把它作为一个字符串放到foreach()中时,在进行打印之后,它工作得很好。当我把它作为字符串放到foreach()中时,在打印之后,它工作得很好。
$info = json_decode($test, true); 
function tagGen($info){
foreach($info as $key => $value){
 }