Using PHP, I'm looking to get an id passed via the url and then lookup some data in a JSON file... then display the data back on the page.
使用PHP,我希望获得通过url传递的id,然后在JSON文件中查找一些数据……然后在页面上显示数据。
I'll set up the url to be http://mytestapp.php?id=12345678 and then use;
我将url设置为http://mytestapp.php?id = 12345678,然后使用;
$id = $_GET['id'];
to set the id variable. I then have a JSON as below;
设置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"}]
}
]
}
Which i want to search for the id, and then be able to echo the contents of the data out.
我想要搜索id,然后能够回显数据的内容。
I'm reading the JSON and decoding into an array as such;
读取JSON并解码成这样的数组;
$json = file_get_contents('data.json');
$arr = json_decode($json, true);
But i'm not sure how to now read the array, find the data i want based on the id, and then pull out the data so i can display it on the page as follows;
但是我不知道如何读取数组,根据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
Any ideas?
什么好主意吗?
Thanks Rob.
谢谢抢劫。
2 个解决方案
#1
2
Borrowed the example from @RobbieAverill and modified to suit your needs, please check if this works.
借鉴@RobbieAverill的例子并修改以满足您的需要,请检查是否有效。
<?php
$id = $_GET['id'];
$json = file_get_contents('data.json');
$foundAd = null;
$json = json_decode($json,true);
foreach ($json['ads'] as $ad) {
if ($ad['id'] == $id) {
$foundAd = $ad;
break;
}
}
echo "Hirer:".$foundAd['hirername']."<br/>";
echo "contact:".$foundAd['hirercontact']."<br/>";
echo "role:".$foundAd['role']."<br/><br/>";
echo "Requirements<br/>";
echo "<ul>";
foreach($foundAd['requirements'] as $req){
echo "<li>".$req['req']."</li>";
}
echo "</ul><br/>";
foreach($foundAd['candidates'] as $req){
echo $req['name']." ". $req['dist']."</br>";
echo $req['exp1']."</br>";
echo $req['exp1']."</br>";
echo $req['avail1']."</br>";
if($req['avail2']!=""){
echo $req['avail2']."</br>";;
}
echo $req['call']."</br></br>";
}
?>
#2
2
In your current inplementation you need to loop over all the ads objects like
在当前输入中,需要循环所有的ads对象
foreach ($arr['ads'] as $ad){
if ($ad['id'] == $id){
//do stuff;
}
}
A better implementation would be to use the value of the id as the key of the json object when you store the json. Using something like
更好的实现是在存储json时使用id值作为json对象的键。使用类似
$ads[id] = $yourjsonobject;
Then referencing would just be $arr['ads'][id];
.
那么引用就是$arr['ads'][id];
You can then use multiple foreach or if your keys are knows just use the keys to output the object you need like
然后,您可以使用多个foreach或如果您的键是已知的,那么只需使用键来输出您需要的对象
echo $ad["hirername"];
Using the foreach
loop to print the complete object:
使用foreach循环打印完整对象:
foreach( $ad as $value){
print_r($value);
}
#1
2
Borrowed the example from @RobbieAverill and modified to suit your needs, please check if this works.
借鉴@RobbieAverill的例子并修改以满足您的需要,请检查是否有效。
<?php
$id = $_GET['id'];
$json = file_get_contents('data.json');
$foundAd = null;
$json = json_decode($json,true);
foreach ($json['ads'] as $ad) {
if ($ad['id'] == $id) {
$foundAd = $ad;
break;
}
}
echo "Hirer:".$foundAd['hirername']."<br/>";
echo "contact:".$foundAd['hirercontact']."<br/>";
echo "role:".$foundAd['role']."<br/><br/>";
echo "Requirements<br/>";
echo "<ul>";
foreach($foundAd['requirements'] as $req){
echo "<li>".$req['req']."</li>";
}
echo "</ul><br/>";
foreach($foundAd['candidates'] as $req){
echo $req['name']." ". $req['dist']."</br>";
echo $req['exp1']."</br>";
echo $req['exp1']."</br>";
echo $req['avail1']."</br>";
if($req['avail2']!=""){
echo $req['avail2']."</br>";;
}
echo $req['call']."</br></br>";
}
?>
#2
2
In your current inplementation you need to loop over all the ads objects like
在当前输入中,需要循环所有的ads对象
foreach ($arr['ads'] as $ad){
if ($ad['id'] == $id){
//do stuff;
}
}
A better implementation would be to use the value of the id as the key of the json object when you store the json. Using something like
更好的实现是在存储json时使用id值作为json对象的键。使用类似
$ads[id] = $yourjsonobject;
Then referencing would just be $arr['ads'][id];
.
那么引用就是$arr['ads'][id];
You can then use multiple foreach or if your keys are knows just use the keys to output the object you need like
然后,您可以使用多个foreach或如果您的键是已知的,那么只需使用键来输出您需要的对象
echo $ad["hirername"];
Using the foreach
loop to print the complete object:
使用foreach循环打印完整对象:
foreach( $ad as $value){
print_r($value);
}