php - Is there a better/elegant way of setting array keys to related values? -


let's say, i've got array looks this:

array (     [0] => red     [1] => green     [2] => blue ) 

i want keys of array same related values. result should this:

array (     [red] => red     [green] => green     [blue] => blue ) 

the values of initial array unique, not issue.

to result, use foreach loop:

$aresult = array(); foreach($acolors $svalue) {   $aresult[$svalue] = $svalue; } 

my question is: there better/elegant way of doing ?

edit: many of want know why need array this. i'll explain. i'm using framework, generates select box me. function uses array keys , values apply them select options so:

<select>   <option value="red">red</option>   <option value="green">green</option>   <option value="blue">blue</option> </select> 

in case keys match values.

$aresult = array_combine($acolours, $acolours); 

but pretty pointless because doesn't give original array doesn't give you.

edit

you may find following variants useful:

$acolours = array('red','orange','yellow','green','blue','indigo','violet');  $aresult = array_combine(     $acolours,     array_map(         'ucfirst',         $acolours     ) );  var_dump($aresult); 

uses lower-case values defined in initial array keys, generates upper-case first letter display values

$acolours = array('red','orange','yellow','green','blue','indigo','violet');  $aresult = array_combine(     array_map(         'strtolower',         $acolours     ),     $acolours );  var_dump($aresult); 

uses mixed-case display values defined in initial array values, generates lower-case keys


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -