tags:

views:

54

answers:

2

I have an array like this:

$options[0] = 1;
$options[1] = 2;
$options[2] = 3;
$options[3] = 'something';

How can I have the value of each array element put as the key, so the array looks like this:

$options[1] = 1;
$options[2] = 2;
$options[3] = 3;
$options['something'] = 'something';

Is there a built in function for this?

+6  A: 
$new = array_combine(array_values($old), array_values($old));
chaos
Is there an equivalent for PHP 4? My client refuses to upgrade
Click Upvote
No. I guess you're stuck with `$new = array(); foreach($old as $val) $new[$val] = $val;`.
chaos
A: 

I think this is a better answer.

$array = array_flip($array);

http://php.net/array%5Fflip

also, there are variations of the function in the comments on that page that you can use if you're on PHP 4

iddqd