note 43515 added to function.array-intersect
| From: | t dot wiltzius at insightbb dot com | Date: | Thu, 24 Jun 2004 01:33:38 +0000 |
| Subject: | note 43515 added to function.array-intersect | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-71563@lists.php.net to get a copy of this message | ||
I needed to compare an array with associative keys to an array that contained some of the keys to
the associative array. Basically, I just wanted to return only a few of the entries in the original
array, and the keys to the entries I wanted were stored in another array. This is pretty
straightforward (although complicated to explain), but I couldn't find a good function for
comparing values to keys. So I wrote this relatively straightforward one:
<?php
function key_values_intersect($values,$keys) {
foreach($keys AS $key) {
$key_val_int[$key] = $values[$key];
}
return $key_val_int;
}
$big =
array("first"=>2,"second"=>7,"third"=>3,"fourth"=>5);
$subset = array("first","third");
print_r(key_values_intersect($big,$subset));
?>
This will return:
Array ( [first] => 2 [third] => 3 )
----
Manual Page -- http://www.php.net/manual/en/function.array-intersect.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43515
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43515&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43515&report=yes
Search -- http://master.php.net/manage/user-notes.php