note 46777 added to function.array-intersect-key
| From: | anarcat at YOUKNOWHATODOWITISanarcat dot ath dot cx | Date: | Fri, 22 Oct 2004 20:07:05 +0000 |
| Subject: | note 46777 added to function.array-intersect-key | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-78960@lists.php.net to get a copy of this message | ||
Behold!
An implementation of this function for PHP < CVS!
<?php
if (!function_exists("array_intersect_key")) {
/**
* Computes the intersection of arrays using keys for comparison
*
* implementation of PHP' CVS array_intersect_key()
*
* array_intersect_key() returns an array containing all the values
* of array1 which have matching keys that are present in all the
* arguments.
*
* might not be exactly equivalent with php.net/array_intersect_key
* since we do not use === for comparison.
*
* will trigger an warning and return FALSE if one of the arguments
* is not an array or if less than one argument is given.
*
* @see http://php.net/array_intersect_key
*
* @author: The Anarcat
* @license: public domain
*/
function array_intersect_key() {
$arrays =& func_get_args();
// if only one array is given as argument, just return it
if (count($arrays) == 1) return $arrays;
elseif (count($arrays) < 1) {
trigger_error("not enough arguments given, ".
count($arrays) ." given, > 1 needed", E_USER_WARNING);
return FALSE;
}
$array1 = array_shift($arrays);
foreach ($array1 as $key => $val) {
for ($i = 0; $i < count($arrays); $i++) {
$array =& $arrays[$i];
if (!is_array($array)) {
trigger_error("argument $i is not an array", E_USER_WARNING);
return FALSE;
}
if (!isset($array[$key])) {
unset($array1[$key]);
}
}
}
return $array1;
}
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.array-intersect-key.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+46777
Delete -- http://master.php.net/manage/user-notes.php?action=delete+46777&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46777&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46777&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46777&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46777&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46777&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+46777&report=yes
Search -- http://master.php.net/manage/user-notes.php