[英]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()
錯誤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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.