note 115006 added to function.array-intersect-key
| From: | github.com/xmarcos | Date: | Mon, 12 May 2014 05:47:48 +0000 |
| Subject: | note 115006 added to function.array-intersect-key | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199515@lists.php.net to get a copy of this message | ||
Here is a better way to merge settings using some defaults as a whitelist.
<?php
$defaults = [
'id' => 123456,
'client_id' => null,
'client_secret' => null,
'options' => [
'trusted' => false,
'active' => false
]
];
$options = [
'client_id' => 789,
'client_secret' => '5ebe2294ecd0e0f08eab7690d2a6ee69',
'client_password' => '5f4dcc3b5aa765d61d8327deb882cf99', // ignored
'client_name' => 'IGNORED', // ignored
'options' => [
'active' => true
]
];
var_dump(
array_merge_recursive($defaults,
array_intersect_key(
$options, $defaults
)
)
);
?>
Output:
array (size=4)
'id' => int 123456
'client_id' => int 789
'client_secret' => string '5ebe2294ecd0e0f08eab7690d2a6ee69' (length=32)
'options' =>
array (size=2)
'trusted' => boolean false
'active' => boolean true
----
Server IP: 72.52.91.14
Probable Submitter: 72.52.91.14
----
Manual Page -- http://php.net/manual/en/function.array-intersect-key.php
Edit -- https://master.php.net/note/edit/115006
Del: integrated -- https://master.php.net/note/delete/115006/integrated
Del: useless -- https://master.php.net/note/delete/115006/useless
Del: bad code -- https://master.php.net/note/delete/115006/bad+code
Del: spam -- https://master.php.net/note/delete/115006/spam
Del: non-english -- https://master.php.net/note/delete/115006/non-english
Del: in docs -- https://master.php.net/note/delete/115006/in+docs
Del: other reasons-- https://master.php.net/note/delete/115006
Reject -- https://master.php.net/note/reject/115006
Search -- https://master.php.net/manage/user-notes.php