这是我要搜索的array
$contacts =array(
array( "name"=>"Samatha Perera", "Email"=>"smatha@gmail.com", "Telephone"=>array("office"=>"0112233789","personal"=>"0718256385","home"=>"0112843425")),
array( "name"=>"Charith Fernando", "Email"=>"charith@gmail.com", "Telephone"=>array("office"=>"0112656457","personal"=>"0718456723","home"=>"0112334466")),
array( "name"=>"Malith Wijekoon", "Email"=>"malith@hotmail.com", "Telephone"=>array("office"=>"0332301311","personal"=>"077456453","home"=>"0112556677")),
array( "name"=>"Sanath Perera", "Email"=>"sanath@yahoo.com", "Telephone"=>array("office"=>"0112201654","personal"=>"0757277365","home"=>"0312867725")),
array( "name"=>"Gihan Indran", "Email"=>"gihan@gmail.com", "Telephone"=>array("office"=>"032222526","personal"=>"0778856385","home"=>"0322243426")),
);
表单将如下所示
搜索方式:名称或电子邮件或电话(这是一个组合框)
搜索值:_______________(搜索按钮)
我想在表格中搜索并显示结果,如下所示。
-----------------------------------
Name | Email | Telephone
Samatha Perera smatha@gmail.com Office:0112233789
Personal:0718256385
Home:0112843425
答案 0 :(得分:0)
从我看到它的方式来看,你需要遍历每个数组并比较这些值。
//$search_term = 'Samatha Perera';
$search_term = '032222526';
foreach ($contacts AS $record) {
foreach ($record AS $key => $val) {
$match_found = false;
if ($key == 'Telephone') {
if (in_array($search_term, $record['Telephone'])) {
$match_found = true;
}
}
else {
if (preg_match('/'.$search_term.'/i', $val)) {
$match_found = true;
}
}
if ($match_found) {
$name_length = strlen($record['name']);
$email_length = strlen($record['Email']);
$total_pad = $name_length + $email_length + 6;
print "\n".$record['name'].' | '.$record['Email'].' | '.$record['Telephone']['office']."\n\n".
str_pad(' ', $total_pad, ' ', STR_PAD_LEFT).$record['Telephone']['personal']."\n\n".
str_pad(' ', $total_pad, ' ', STR_PAD_LEFT).$record['Telephone']['home']."\n";
}
}
}
答案 1 :(得分:0)
试试这个......下面的代码会匹配任何文字......
//$search = 'Samatha';
//$search = '011';
$search = 'Ferna';
$match = array();
?>
<table>
<tr>
<td>Name</td>
<td>Email</td>
<td>Phone</td>
</tr>
<?php
foreach($contacts as $key => $value) {
if(strpos($value['name'],$search) !== false || strpos($value['Email'],$search) !== false) {
$match[] = $value;
continue;
}
foreach($value['Telephone'] as $k => $v) {
if(strpos($v,$search) !== false) {
$match[] = $value;
break;
}
}
}
if(!empty($match))
foreach($match as $key => $value) {
?>
<tr>
<td><?php echo $value['name']?></td>
<td><?php echo $value['Email']?></td>
<td><?php
foreach($value['Telephone'] as $k => $v) {
echo "{$k} : {$v}<br />";
}
?></td>
</tr>
<?php
}
?></table>