Colin Ramsay
Colin Ramsay

Reputation: 16476

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.

Upvotes: 1

Views: 931

Answers (2)

Tom Haigh
Tom Haigh

Reputation: 57815

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

Upvotes: 5

Palantir
Palantir

Reputation: 24182

array_flip() ?

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

Upvotes: -2

Related Questions