[英]How to get nested values from json in php
我有一个 json 文件我想用作数据库只是因为我喜欢 json 的结构而不是 mysql php 我有以下代码
$query = "people.json";
$json = file_get_contents($query);
$data = json_decode($json, true);
$faq = $data['FAQ'];
$questions = $faq['Questions']['Number'];
foreach($questions as $question){
$number = $question['number'];
$question = $question['question'];
$answer = $question['answer'];
?>
{
"FAQ":{
"Questions":{
"Number":{
"1":{
"number":"one",
"question":"How do i trade with Marketwh steam bots?",
"answer":"You can Trade one of 2 ways <br> Send a Trade offer to one of our Steam Bots <br> & #8226; find the item on our site see the price of it and then click trade and send a trade offer like normal."
},
"2":{
"number":"two",
"question":"What is a Steam Bot?",
"answer":"A Steam Bot is an automated Trading Bot that will automatcally accept offers sent to it that are within the item price range of buying or selling."
},
"3":{
"number":"three",
"question":"What is a Steam Bot?",
"answer":"A Steam Bot is an automated Trading Bot that will automatcally accept offers sent to it that are within the item price range of buying or selling."
},
"4":{
"number":"four",
"question":"",
"answer":""
},
"5":{
"number":"five",
"question":"",
"answer":""
}
}
}
}
}
$number 输出“一”
$question 输出问题
$answer 输出问题变量中的第一个字母,第一个是“H”,第二个和第三个答案是“W”。 正如你在上面的代码中看到的,我把它放在一个 foreach 中,它应该循环遍历 FAQ 数组中的每个数字 1-4
我正在使用它来调用数据库的 php 我可以编辑和更改我的网站的内容,常见问题是我将使用的常见问题页面
$questions 是一个对象,而不是一个列表。 我希望您需要更改 json 格式,以便将问题列在列表中。 像下面这样:
{
"FAQ": {
"Questions": {
"Number": [
{
"number": "one",
"question": "How do i trade with Marketwh steam bots?",
"answer": "You can Trade one of 2 ways <br> Send a Trade offer to one of our Steam Bots <br> & #8226; find the item on our site see the price of it and then click trade and send a trade offer like normal."
},
{
"number": "two",
"question": "What is a Steam Bot?",
"answer": "A Steam Bot is an automated Trading Bot that will automatcally accept offers sent to it that are within the item price range of buying or selling."
},
{
"number": "three",
"question": "What is a Steam Bot?",
"answer": "A Steam Bot is an automated Trading Bot that will automatcally accept offers sent to it that are within the item price range of buying or selling."
},
{
"number": "four",
"question": "",
"answer": ""
},
{
"number": "five",
"question": "",
"answer": ""
}
]
}
}
}
正如user3720435所说,更新您的 JSON 以使Number
成为array ([])
而不是Object ({})
。 您无法像您想要的那样迭代对象,但是,这可以通过数组来实现。 有关您的具体问题的信息,请参阅以下内容。
我在原始问题中
$query = "people.json";
$json = file_get_contents($query);
$data = json_decode($json, true);
$faq = $data['FAQ'];
$questions = $faq['Questions']['Number'];
foreach($questions as $question){
$number = $question['number'];
$question = $question['question'];
$answer = $question['answer'];
?>
我像这样更改了php中的代码
$data = json_decode($json, true);
$questions = $data['FAQ']['Questions']['Number'];
foreach($questions as $quest){
$number = $quest['number'];
$question = $quest['question'];
$answer = $quest['answer'];
?>
我在 foreach 中调用了 $question ,之后我将 foreach 中的 $question 更改为 $quest 并修复了它,一旦我意识到它可能像那样和 json 冲突,我问的原始方式和我确实尝试过 [{}] 方法将它作为一个数组来做,但它需要一个对象,输出就像我想要的一样感谢您的答案,我很高兴我发现出了什么问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.