$args = array(‘first_name‘=>‘Brenda‘,‘last_name‘=>‘Buzzard‘,‘name‘=>‘Flank‘); $values[] = array(‘first_name‘=>‘Brenda‘,‘last_name‘=>‘Buzzard‘,‘name‘=>‘Flank‘); $values[] = array(‘first_name‘=>‘Victor‘,‘last_name‘=>‘Vulture‘,‘name‘=>‘Frank‘); $values[] = array(‘first_name‘=>‘Bill‘,‘last_name‘=>‘Blue Jay‘); $values[] = array(‘first_name‘=>‘Bill‘,‘last_name‘=>‘Buzzard‘); $result = search_for($values,$args); 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; }
手册里的评论,关于灵活使用array_product和array_key_exists函数来搜寻一个数组是否在另一个数组里。感觉方法挺不错,做个记录!
时间: 2024-10-08 03:23:10