我正在尝试编写一个php函数,该函数显示给出ID的部分查询的人。我目前有代码工作接受ID的完全查询,但无法找到一种方法来操纵我的代码返回部分字符串的值。
守则
function display_person($id) {
global $people;
if(array_key_exists($id, $people)) {
header('Content-type: application/json');
echo json_encode($people[$id]);
} else {
header('HTTP/1.1 404 Not Found');
}
}
数组
$people = array(
'jjones' => array(
'firstName' => 'Jim',
'lastName' => 'Jones',
'age' => 20,
'major' => 'Computer Science',
'phone' => '212-460-9393',
'email' => 'jjones@miamioh.edu',
'state' => 'OH'
),
'asmith' => array(
'firstName' => 'April',
'lastName' => 'Smith',
'age' => 19,
'major' => 'Mechanical Engineering',
'phone' => '913-939-3929',
'email' => 'asmith@miamioh.edu',
'state' => 'WY'
),
'pstemple' => array(
'firstName' => 'Pat',
'lastName' => 'Stemple',
'age' => 21, 'major' =>
'Theater Performance',
'phone' => '917-222-2232',
'email' => 'pstemple@miamioh.edu',
'state' => 'NY'
),
'jjones1' => array(
'firstName' => 'Janet',
'lastName' => 'Jones',
'age' => 22,
'major' => 'Botany',
'phone' => '817-332-9392',
'email' => 'jjones1@miamioh.edu',
'state' => 'CA'
),
'llerner' => array(
'firstName' => 'Leon',
'lastName' => 'Lerner',
'age' => 18,
'major' => 'Biology',
'phone' => '315-444-3494',
'email' => 'llerner@miamioh.edu',
'state' => 'OH'
),
'mmeyer' => array(
'firstName' => 'Margret',
'lastName' => 'Meyer',
'age' => 24,
'major' => 'Interactive Media Studies',
'phone' => '219-333-0303',
'email' => 'mmeyer@miamioh.edu',
'state' => 'OH'
),
'achaudhry' => array(
'firstName' => 'Anik',
'lastName' => 'Chaudhry',
'age' => 19,
'major' => 'Management Information Systems',
'phone' => '914-555-5555',
'email' => 'achaudhry@miamioh.edu',
'state' => 'NY'
),
'sdogg' => array(
'firstName' => 'Snoop',
'lastName' => 'Dogg',
'age' => 42,
'major' => 'Botany',
'phone' => '414-333-2433',
'email' => 'sdogg@miamioh.edu',
'state' => 'CA'
),
'bclinton' => array(
'firstName' => 'Bill',
'lastName' => 'Clinton',
'age' => 25,
'major' => 'Political Science',
'phone' => '933-440-3033',
'email' => 'bclinton@miamioh.edu',
'state' => 'AK'
)
);
答案 0 :(得分:3)
如果您只想提供1个输出,可以稍微修改一下代码:
function display_person($query) {
global $people;
$foundid = false;
foreach ($people as $k => $v)
if (stripos($k, $query) !== false)
{
$foundid = $k;
break;
}
if($foundid !== false) {
header('Content-type: application/json');
echo json_encode($people[$foundid]);
} else {
header('HTTP/1.1 404 Not Found');
}
}
另一种方法是找到所有可能的变体(为此你需要临时数组)
function display_person($query) {
global $people;
$foundid = array();
foreach ($people as $k => $v)
if (stripos($k, $query) !== false)
{
$foundid[$k] = $v;
}
if(count($foundid) > 0) {
header('Content-type: application/json');
echo json_encode($foundid); // NOTE: you need to change your JS code to accept array instead of 1 person
} else {
header('HTTP/1.1 404 Not Found');
}
}
另外,我建议你检查$ query是否足够长,通常是3+符号
答案 1 :(得分:2)
similar_text()
作为示例...... 为匹配项之间的相似性设置 80 的百分比,这些匹配的字符串将添加到$sim
数组中。然后,您可以json_encode()
最终。
function display_person_new($id,$people) {
$arr = array_keys($people);
foreach($arr as $v)
{
similar_text($id,$v,$percent);
if($percent>80)
{
$sim[$v]=$people[$v];
}
}
if(!empty($sim))
{
header('Content-type: application/json');
echo json_encode($sim,JSON_PRETTY_PRINT);
} else { header('HTTP/1.1 404 Not Found'); }
}
display_person_new('jjone',$people);
<强> OUTPUT :
强>
{
"jjones": {
"firstName": "Jim",
"lastName": "Jones",
"age": 20,
"major": "Computer Science",
"phone": "212-460-9393",
"email": "jjones@miamioh.edu",
"state": "OH"
},
"jjones1": {
"firstName": "Janet",
"lastName": "Jones",
"age": 22,
"major": "Botany",
"phone": "817-332-9392",
"email": "jjones1@miamioh.edu",
"state": "CA"
}
}
答案 2 :(得分:1)
首先,创建一个匹配键数组。例如
$matchingIds = array_filter(array_keys($people), function($pid) use ($id) {
// perform a case insensitive search for $id in $pid
return stripos($pid, $id) !== false;
});
然后,将此数组与您的$people
键相交
$matches = array_intersect_key($people, array_flip($matchingIds));
// array_flip allows us to flip the matching ID values into keys
// so we can intersect with the $people keys
if (count($matches)) {
header('Content-type: application/json');
echo json_encode($matches);
} else {
http_response_code(404);
echo 'No matches found';
}
最后,我会废除global
变量。使用它们会使测试变得困难并增加对全局状态的依赖(这很糟糕)。相反,在调用它时将$people
数组注入函数
function display_person($id, array $people) {
...
}
答案 3 :(得分:0)
function display_person($id)
{
global $people;
foreach ($people as $key => $value)
{
if (strpos($key, $id) === 0)
{
header('Content-type: application/json');
echo json_encode($people[$key]) . "<br>";
} else
{
header('HTTP/1.1 404 Not Found');
}
}
}