简体   繁体   中英

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM