【发布时间】:2015-10-21 03:08:40
【问题描述】:
使用 PHP,我正在寻找通过 url 传递的 id,然后在 JSON 文件中查找一些数据...然后将数据显示回页面上。
我将网址设置为http://mytestapp.php?id=12345678,然后使用;
$id = $_GET['id'];
设置 id 变量。然后我有一个如下的 JSON;
{
"ads":
[
{ "id":"12345678",
"hirername":"Test Hirer",
"hirercontact":"Rob A",
"role":"Ultra Sat Role",
"requirements": [
{"req":"Right to work in Australia"},
{"req":"Live locally"}],
"candidates": [
{"name":"John Smith","dist":"Nunawading (23km away)","exp1":"Pizza maker at Don Domenicos for 4 years","exp2":"Bakery Assistant at Woolworths for 4 years","req":"","avail1":"Mon to Fri | Morning, Evening & Night","avail2":"","call":"0413451007"},
{"name":"Jack Smith","dist":"Endeadvour Hills (35km away)","exp1":"Pizzaiolo (Pizza maker) at Cuor Di Pizza for 1 year","exp2":"","req":"","avail1":"Mon to Fri | Morning & Evening","avail2":"","call":"041345690"}]
},
{ "id":"12345679",
"hirername":"Test Hirer 2",
"hirercontact":"Jesse S",
"role":"Ultra Sat Role 2",
"requirements": [
{"req":"Right to work in Australia"},
{"req":"Live locally"}],
"candidates": [
{"name":"Jill Smith","dist":"Nunawading (23km away)","exp1":"Pizza maker at Don Domenicos for 4 years","exp2":"Bakery Assistant at Woolworths for 4 years","req":"","avail1":"Mon to Fri | Morning, Evening & Night","avail2":"","call":"0413451007"},
{"name":"Jenny Smith","dist":"Endeadvour Hills (35km away)","exp1":"Pizzaiolo (Pizza maker) at Cuor Di Pizza for 1 year","exp2":"","req":"","avail1":"Mon to Fri | Morning & Evening","avail2":"","call":"041345690"}]
}
]
}
我想搜索其中的id,然后能够将数据的内容回显出来。
我正在读取 JSON 并解码成这样的数组;
$json = file_get_contents('data.json');
$arr = json_decode($json, true);
但我现在不知道如何读取数组,根据id找到我想要的数据,然后把数据拉出来,这样我就可以在页面上显示如下;
Hirer: Test Hirer
Contact: Rob A
Role: Ultra Sat Role
Requirements:
- Right to work in Australia
- Live Locally
John Smith Nunawading (23km away)
Pizza maker at Don Domenicos for 4 years
Bakery Assistant at Woolworths for 4 years
Mon to Fri | Morning, Evening & Night
0413451007
Jack Smith Endeadvour Hills (35km away)
Pizzaiolo (Pizza maker) at Cuor Di Pizza for 1 year
Mon to Fri | Morning & Evening
041345690
有什么想法吗?
谢谢罗。
【问题讨论】:
-
你能补充一下你是如何阅读它的吗?你从哪里得到 json
-
我已经更新了。谢谢。
-
你的 json 无效
-
哦,是的 - 我已经更新了。应该是有效的。谢谢。
-
感谢@SusheelSingh,但我故意将数据存储在 JSON 文件中,而不是创建读取数据并返回单个对象的服务。 (创建一个 hacky 应用程序进行测试)。