note 114936 added to function.array-product
| From: | JimmyPHP | Date: | Tue, 29 Apr 2014 19:55:16 +0000 |
| Subject: | note 114936 added to function.array-product | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199374@lists.php.net to get a copy of this message | ||
array_product() can be used to implement a simple boolean AND search
<?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;
}
?>
Output:
array (size=1)
0 =>
array (size=2)
'first_name' => string 'Bill' (length=4)
'last_name' => string 'Buzzard' (length=7)
----
Server IP: 72.52.91.14
Probable Submitter: 24.89.139.58
----
Manual Page -- http://php.net/manual/en/function.array-product.php
Edit -- https://master.php.net/note/edit/114936
Del: integrated -- https://master.php.net/note/delete/114936/integrated
Del: useless -- https://master.php.net/note/delete/114936/useless
Del: bad code -- https://master.php.net/note/delete/114936/bad+code
Del: spam -- https://master.php.net/note/delete/114936/spam
Del: non-english -- https://master.php.net/note/delete/114936/non-english
Del: in docs -- https://master.php.net/note/delete/114936/in+docs
Del: other reasons-- https://master.php.net/note/delete/114936
Reject -- https://master.php.net/note/reject/114936
Search -- https://master.php.net/manage/user-notes.php