Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace array key by its value

Tags:

arrays

php

I have the following array:

array('Elnett', 'INOA INOA', 'Playball P', 'Preferred Color Specialist', 
      'Série Expert', 'Série Nature', 'Techni 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?

like image 727
Mamadou Avatar asked May 31 '11 09:05

Mamadou


2 Answers

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);
like image 88
GolezTrol Avatar answered Sep 27 '22 19:09

GolezTrol


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.

like image 32
Positivity Avatar answered Sep 27 '22 18:09

Positivity