开发者

PHP Multidimensional Array - "Exchange" Dimensions

I'm wondering what the best way is of doing this:

$fc['abc'][0] = 1;
$fc['xyz'][0] = 2;
$fc['abc'][1] = 3;
$fc['xyz'][1] = 4;

$fc2 = something开发者_运维技巧($fc);

print $fc2[0]['abc']; // 1

In other words, the something function will swap the two dimensions round.


There is probably a more elegant way of doing this, but this works:

$result = array();
foreach ($fc as $key1 => $arr) {
    foreach ($arr as $key2 => $num) {
        $result[$key2][$key1] = $num;
    }
}


array_flip() ?

http://php.net/manual/en/function.array-flip.php

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜