PHP transform array from one dimenson to two
some "simple" problem:
I've this array;
$myArray = array(
'FOO',
'BAR,
);
i want :
$mayArray = array(
'FOO' => array(),
'BAR' => array(),
);
in the mome开发者_如何学运维nt iam doing it with an foreach:
foreach ($myArray as $key => $val) {
$newArray[$val] = array();
}
$myArray = $newArray;
is there an easyer way ? ;-)
The way you have is pretty easy to understand. But you can also do this:
$myArray = array_fill_keys($myArray, array());
Docs here: http://us2.php.net/manual/en/function.array-fill-keys.php
You can use array_fill_keys, here is example:
$myArray = array_fill_keys($myArray, array());
精彩评论