我正在尝试编写一个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'
)
);