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" => "");
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" => "");
you can use foreach
to produce a new array, or one of numerous array functions
return array_fill_keys(array_keys($array1), "");
(Example run: http://www.ideone.com/SuMt2)
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));