我有Json文件,其中包含很多数组。如何通过名称(例如get参数)为每个人动态获取数组。
的Json
[{
"Name": "Somename",
"Lastname": "somelastname",
"Address": "someaddress",
},
{
"Name": "Somename1",
"Lastname": "somelastname1",
"Address": "someaddress1",
},
{
"Name": "Somename2",
"Lastname": "somelastname2",
"Address": "someaddress2",
}]
PHP
<?php
error_reporting(0);
$json_file = file_get_contents('jsonfile.json');
$someArray = json_decode($json_file, true);
?>
HTML
<a href="product.php?post=name">
<h3 id="custompage"><?php echo $value["Name"]; ?>
<span><br><?php echo $value["Lastname"]; ?></span>
<span><?php echo $value["Address"]; ?></span>
</h3>
</a>
答案 0 :(得分:0)
<?php
$findname = "John2";
$findlastname = "Doe2";
$data = '[
{
"Name": "John1",
"Lastname": "Doe1",
"Address": "1 Fake St"
},
{
"Name": "John2",
"Lastname": "Doe2",
"Address": "2 Fake St"
},
{
"Name": "John3",
"Lastname": "Doe3",
"Address": "3 Fake St"
}
]';
$array=json_decode($data);
foreach($array as $arraydata)
{
if($arraydata->Name==$findname && $arraydata->Lastname==$findlastname){
$address = $arraydata->Address;
}
}
echo "Adress of ".$findname." ".$findlastname." is:".$address;
?>
答案 1 :(得分:-1)
<?php
$json = '[{ "Name": "Somename", "Lastname": "somelastname", "Address": "someaddress", }, { "Name": "Somename1", "Lastname": "somelastname1", "Address": "someaddress1", }, { "Name": "Somename2", "Lastname": "somelastname2", "Address": "someaddress2", }]';
$arr = json_decode($json,true);
$needle = "Somename";
$keyfound = "";
foreach($arr as $k=>$sub) {
if($sub['Name']==$needle) {
$keyfound = $k;
}
}
echo "name ".$needle." found in key ".$keyfound.";
?>