How to replace array key by its value
I have the following array:
array('Elnett', 'INOA INOA', 'Playball P', 'Preferred Color Specialist',
'Série Expert', 'Série Nature', 'Tech开发者_C百科ni art')
I would like to have keys and values like:
array('Elnett' => 'Elnett',
'INOA INOA' => 'INOA INOA',
'Playball P' => 'Playball',
'Preferred Color Specialis' => 'Preferred Color Specialist',
'Série Expert' => 'Série Expert',
'Série Nature' => 'Série Nature',
'Techni art' => 'Techni art')
How can I accomplish this?
There's array_combine to create a key/value array out of two arrays. Should be possible to use the same array for keys and values:
$names = array_combine($names, $names);
This one-line answer may be useful to someone.
$trans = array_flip($array);
http://us1.php.net/array_flip
array_flip returns the value=>key format of given key=>value array. as the question changed, this does not exactly answer the OP's question anymore.
Could do something like this. Not sure if there is a cleaner way to do it.
foreach($names as $key => $name){
$names[$name] = $name;
unset($names[$key]);
}
foreach($array as $key=>$value){
$out[$value] = $value;
}
print_r($out);
精彩评论