我有一个json数组
<div>
<form id="form" method="POST" action="your link">
. . . . .
<p>
<input type="submit" value="Pay">
</p>
</form>
</div>
如果用户使用任何ssn或名称或数字搜索,我想获取id值。搜索输入可能不是单个值,这意味着它可能是ssn和name的组合,或者可能是name,ssn和number 。任何名称编号的组合,ssn可能是输入。如果输入匹配使用php。我必须得到id值。我使用xpath for xml但我想搜索fron json.can有人建议我有任何想法..谢谢你。 ..
答案 0 :(得分:1)
要通过分析JSON字符串来搜索用户ID,请使用以下方法array_filter
和array_intersect_assoc
函数:
$json_str = '[ { "id":1.25, "name":"rose", "Number":15, "SSN":[ 12345, 3455 ] }, { "id":1.15, "name":"orchid", "Number":7, "SSN":[ 12345, 3455 ] }]';
$decoded = json_decode($json_str, true);
// base search structure
$search_structure = ['name' => "", 'Number' => 0];
// let's say, the following inputs have come from API (some of them may be empty or omitted)
$number = 7;
$ssn = "3455";
if ($number && is_numeric($number)) $search_structure['Number'] = $number;
$search_structure = array_filter($search_structure); // filtering out empty params
$count_params = count($search_structure);
$user_id = 0;
foreach ($decoded as $item) {
if ($count_params == count(array_intersect_assoc($search_structure, $item))) {
if (!$ssn || ($ssn && in_array($ssn, $item['SSN']))) {
$user_id = $item['id'];
break;
}
}
}
print_r(($user_id)? "User id: $user_id" : "User with specified params not found!");
输出:
User id: 1.15
答案 1 :(得分:0)
让$ json包含一个数组字符串
$ar = json_decode($json, true);
$id = false;
foreach ($ar as $i)
if ($i["name"] == "rose" and $i["Number"] == 15)
{ $id = $i["id"]; break; }
if ($id !== false) {
// use $id
}
答案 2 :(得分:0)
优选地,convert the json data to native PHP Object
然后使用foreach,遍历对象并提取与给定SSN号匹配的对象的ID。提取的结果将分配给一个数组。下面是一个评论过程:1可能的解决方法:
<?php
// WE ASSUME, JUST FOR TESTING PURPOSES THAT THE SSN NUMBER WE ARE LOOKING FOR IS 4567
$ssnNumber = 4567; //3455;
// CREATE A NEW ARRAY VARIABLE TO HOLD THE IDs YOU DESIRE & INITIALIZE IT TO AN EMPTY ARRAY
$arrIDS = array();
$jsonData = '[
{
"id" :1.25,
"name" :"rose",
"Number" :15,
"SSN" :[
12345,
3455
]
},
{
"id" :1.15,
"name" :"orchid",
"Number" :7,
"SSN" :[
12345,
3455
]
},
{
"id" :1.75,
"name" :"orchid",
"Number" :79,
"SSN" :[
4567,
89012
]
}
]';
// CONVERT $jsonData TO NATIVE PHP OBJECT
$objJson = json_decode($jsonData);
// LOOP THROUGH THE PHP OBJECT & TEST IF A GIVEN SSN EXIST.
foreach($objJson as $key=>$data){
// CHECK IF THE $data->SSN IS SET:
if(isset($data->SSN) && is_array($data->SSN)){
// CHECK IF THE SSN ARRAY CONTAINS THE GIVEN SSN NUMBER.
if(in_array($ssnNumber, $data->SSN)){
$arrIDS[] = $data->id;
}
}
}
var_dump($arrIDS); // DISPLAY: array (size=1) 0 => float 1.75
或者,也可以将上述例程withina函数封装起来,以便重新使用,如下所示:
<?php
function getIDBySSN($jsonData, $ssnNumber){
$arrIDs = array();
$objJson = json_decode($jsonData);
// LOOP THROUGH THE PHP OBJECT & TEST IF A GIVEN SSN EXIST.
foreach($objJson as $key=>$data){
// CHECK IF THE $data->SSN IS SET:
if(isset($data->SSN) && is_array($data->SSN)){
// CHECK IF THE SSN ARRAY CONTAINS THE GIVEN SSN NUMBER.
if(in_array($ssnNumber, $data->SSN)){
$arrIDs[] = $data->id;
}
}
}
return $arrIDs;
}
var_dump(getIDBySSN($jsonData, $ssnNumber)); // PRODUCES THE SAME RESULT
测试HERE: