note 118719 added to function.array-replace
| From: | ali dot sweden19 at yahoo dot com | Date: | Mon, 25 Jan 2016 14:42:10 +0000 |
| Subject: | note 118719 added to function.array-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-204760@lists.php.net to get a copy of this message | ||
Here is a simple array_replace_keys function:
/**
* This function replaces the keys of an associate array by those supplied in the keys array
*
* @param $array target associative array in which the keys are intended to be replaced
* @param $keys associate array where search key => replace by key, for replacing respective
keys
* @return array with replaced keys
*/
private function array_replace_keys($array, $keys)
{
foreach ($keys as $search => $replace) {
if ( isset($array[$search])) {
$array[$replace] = $array[$search];
unset($array[$search]);
}
}
return $array;
}
// Test Drive
print_r(array_replace_keys(['one'=>'apple',
'two'=>'orange'], ['one'=>'ett',
'two'=>'tvo']);
// Output
array(
'ett'=>'apple',
'tvo'=>'orange'
)
----
Server IP: 72.52.91.14
Probable Submitter: 83.140.242.20
----
Manual Page -- http://php.net/manual/en/function.array-replace.php
Edit -- https://master.php.net/note/edit/118719
Del: integrated -- https://master.php.net/note/delete/118719/integrated
Del: useless -- https://master.php.net/note/delete/118719/useless
Del: bad code -- https://master.php.net/note/delete/118719/bad+code
Del: spam -- https://master.php.net/note/delete/118719/spam
Del: non-english -- https://master.php.net/note/delete/118719/non-english
Del: in docs -- https://master.php.net/note/delete/118719/in+docs
Del: other reasons-- https://master.php.net/note/delete/118719
Reject -- https://master.php.net/note/reject/118719
Search -- https://master.php.net/manage/user-notes.php