tags:

views:

33

answers:

2

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.

A: 

array_flip() ?

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

Palantir
No, this doesn't work because array_flip won't work with multidimensional arrays, you get an error.
colinramsay
+3  A: 

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;
    }
}
Tom Haigh
Yep, I've tested this and it does work. Like you, I'm wondering if there's a more elegant way though!
colinramsay