note 80227 added to function.array-intersect-key
| From: | CBWhiz at gmail dot com | Date: | Fri, 04 Jan 2008 22:04:30 +0000 |
| Subject: | note 80227 added to function.array-intersect-key | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-134783@lists.php.net to get a copy of this message | ||
I have found the following helpful:
<?PHP
function array_merge_default($default, $data) {
$intersect = array_intersect_key($data, $default); //Get data for which a default exists
$diff = array_diff_key($default, $data); //Get defaults which are not present in data
return $diff + $intersect; //Arrays have different keys, return the union of the two
}
?>
It's use is like both of the functions it uses, but keeps defaults and _only_ defaults.
It's designed for key arrays, and i'm not sure how it will work on numeric indexed arrays.
Example:
<?PHP
$default = array(
"one" => 1,
"two" => 2
);
$untrusted = array(
"one" => 42,
"three" => 3
);
var_dump(array_merge_default($default, $untrusted));
array(2) {
["two"]=>
int(2)
["one"]=>
int(42)
}
?>
----
Server IP: 208.69.120.35
Probable Submitter: 24.46.21.192
----
Manual Page -- http://www.php.net/manual/en/function.array-intersect-key.php
Edit -- https://master.php.net/note/edit/80227
Del: integrated -- https://master.php.net/note/delete/80227/integrated
Del: useless -- https://master.php.net/note/delete/80227/useless
Del: bad code -- https://master.php.net/note/delete/80227/bad+code
Del: spam -- https://master.php.net/note/delete/80227/spam
Del: non-english -- https://master.php.net/note/delete/80227/non-english
Del: in docs -- https://master.php.net/note/delete/80227/in+docs
Del: other reasons-- https://master.php.net/note/delete/80227
Reject -- https://master.php.net/note/reject/80227
Search -- https://master.php.net/manage/user-notes.php