[php] Array copy values to keys in PHP

I have this array:

$a = array('b', 'c', 'd');

Is there a simple method to convert the array to the following?

$a = array('b' => 'b', 'c' => 'c', 'd' => 'd');

This question is related to php arrays

The answer is


Be careful, the solution proposed with $a = array_combine($a, $a); will not work for numeric values.

I for example wanted to have a memory array(128,256,512,1024,2048,4096,8192,16384) to be the keys as well as the values however PHP manual states:

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

So I solved it like this:

foreach($array as $key => $val) {
    $new_array[$val]=$val;
}