tags:

views:

54

answers:

3

In PHP, what's the best way to copy an array and keep the keys with empty values?

array1 = array("apple" => "green", "banana" => "yellow);

I want to copy array1 to array2 and keep only the keys...

array2 = array("apple" => "", "banana" => "");
+1  A: 

you can use foreach to produce a new array, or one of numerous array functions

Col. Shrapnel
+6  A: 
return array_fill_keys(array_keys($array1), "");

(Example run: http://www.ideone.com/SuMt2)

KennyTM
+2  A: 

What about array_keys(), it return an array that has the keys of another array as values.

You can then use array_flip() to change keys for values and voilá, you have your result.

In a nutshell:

$array2 = array_flip(array_keys($array1));
Techpriester
He don't want to flip.
elias
Ah nevermind, misread :)
elias