PHP json_decode 和 foreach 无效参数


PHP json_decode and foreach invalid argument

我尝试从我的数据库中foreach一个 json 数组。

以下是数据:

$data = "["B015f6c48c43b7494", "B69036e96dccae075"]";

这是我要执行的步骤:

$result=[];
$decode = json_decode($data, true);
foreach($decode as $row){
   array_push($result, ['id'=>$row]);
}
return $result;

但它Invalid argument supplied for foreach()返回我此错误。

有什么解决办法吗?


我已经尝试dd$decode这是结果:

array:2 [
   0 => "B015f6c48c43b7494"
   1 => "B69036e96dccae075"
]

您的牙套有问题。

请替换

$data = "["B015f6c48c43b7494", "B69036e96dccae075"]";

$data = '["B015f6c48c43b7494", "B69036e96dccae075"]';

这是完整的示例:

$data = '["B015f6c48c43b7494", "B69036e96dccae075"]';
$result=[];
$decode = json_decode($data, true);
foreach($decode as $row){
   array_push($result, ['id'=>$row]);
}
var_dump($result);

它返回:

array (size=2)
  0 => 
    array (size=1)
      'id' => string 'B015f6c48c43b7494' (length=17)
  1 => 
    array (size=1)
      'id' => string 'B69036e96dccae075' (length=17)