7

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?

2
  • Note you can format lines as code by indenting them four spaces. The "{}" button in the editor toolbar does this for you. If you want to preserve whitespace (indentation and new lines) for text that isn't code (such as the sample arrays), use <pre> elements. Edit your question and try it out. Click the orange question mark in the editor toolbar for more information and tips on formatting. Commented May 31, 2011 at 9:26
  • Also, complete, concise sample code is always more helpful than a dump. Commented Jun 1, 2011 at 0:49

4 Answers 4

31

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);
Sign up to request clarification or add additional context in comments.

Comments

10

This one-line answer may be useful to someone.

$trans = array_flip($array);

https://www.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.

2 Comments

between this and the other answer here you can start with $a = array( 'foo' => 'bar' ) and do (1) array_combine($a, $a) to get array( 'bar' => 'bar' ), (2) array_flip($a) to get array( 'bar' => 'foo' ), (3) array_combine( array_flip($a), array_flip($a) ) to get array( 'foo' => 'foo' )
Just watch out for array_flip when all of your keys are unique, but your values contain duplicates. You will end up with a smaller array than you started with, which may or may not have been your intention.
1

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]);
}

1 Comment

What about this array: array(0, 1, 2) :)
-1
foreach($array as $key=>$value){
    $out[$value] = $value;
}

print_r($out);

3 Comments

Wrong answer. The first array ($array) was like this: [0=>'Elnett', 1=>'INOA INOA', ...]. After these transformations we get this array ($out): [0=>'Elnett', 1=>'INOA INOA', 'Elnett'=>'Elnett', 'INOA INOA'=>'INOA INOA', ...].
did you even test this code? what happens to the original keys? this will just duplicate your array
As the $out is a fresh array, the original keys are not in there.. you can see the tinker here: onlinephp.io/c/15b84

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.