array_product() 函式可以用來實作簡單的布林值 AND 搜尋
<?php
$args = array('first_name'=>'Bill','last_name'=>'Buzzard');
$values[] = array('first_name'=>'Brenda','last_name'=>'Buzzard');
$values[] = array('first_name'=>'Victor','last_name'=>'Vulture');
$values[] = array('first_name'=>'Bill','last_name'=>'Blue Jay');
$values[] = array('first_name'=>'Bill','last_name'=>'Buzzard');
$result = search_for($values,$args);
var_dump($result);exit;
function search_for($array,$args) {
$results = array();
foreach ($array as $row) {
$found = false;
$hits = array();
foreach ($row as $k => $v) {
if (array_key_exists($k,$args)) $hits[$k] = ($args[$k] == $v);
}
$found = array_product($hits);
if (!in_array($row,$results) && true == $found) $results[] = $row;
}
return $results;
}
?>
輸出:
陣列 (大小=1)
0 =>
陣列 (大小=2)
'first_name' => 字串 'Bill' (長度=4)
'last_name' => 字串 'Buzzard' (長度=7)