我从我网站上生成的表单中构建了这个JSON。 我现在想将单个项目推送到数据库,所以我需要能够通过迭代来提取指令。
$ instruction0 $指令1
和坐在教学中的成分。
当尝试解析JSON时出现错误,我不知道如何在PHP中读取它。 (AKA PHP NOOB)我能够通过Python获得我所需的所有项目而没有任何问题。
[
[
{
"instruction": "Enter text here...asdada"
},
{
"ingredient": "Beetroot",
"amount": "2",
"type": "grams"
},
{
"ingredient": "Beetroot",
"amount": "1",
"type": "grams"
}
],
[
{
"instruction": "Enter text here..sdfsdf."
},
{
"ingredient": "Carrot",
"amount": "2",
"type": "grams"
},
{
"ingredient": "Beetroot",
"amount": "525",
"type": "grams"
}
]
]
[
[
{
"instruction": "Enter text here...asdada"
},
{
"ingredient": "Beetroot",
"amount": "2",
"type": "grams"
},
{
"ingredient": "Beetroot",
"amount": "1",
"type": "grams"
}
],
[
{
"instruction": "Enter text here..sdfsdf."
},
{
"ingredient": "Carrot",
"amount": "2",
"type": "grams"
},
{
"ingredient": "Beetroot",
"amount": "525",
"type": "grams"
}
]
]
答案 0 :(得分:0)
您是否尝试使用第二个参数“true” json_decode 来指定您希望将输出作为关联数组。
json_decode($json, true);
其次,如果你手动将这个json字符串发布到你的代码中,你需要将它包装在引号内,所以基于那个
$json = '[ [ { "instruction": "Enter text here...asdada" }, { "ingredient": "Beetroot", "amount": "2", "type": "grams" }, { "ingredient": "Beetroot", "amount": "1", "type": "grams" } ], [ { "instruction": "Enter text here..sdfsdf." }, { "ingredient": "Carrot", "amount": "2", "type": "grams" }, { "ingredient": "Beetroot", "amount": "525", "type": "grams" } ] ] ';
$array = json_decode($json, true);
然后您可以正常迭代$array