开发者

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());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜