PHP数组改变数组结构

时间:2011-04-11 16:34:16

标签: php arrays

我有一个以下形式的数组,

Array
(
    [foo] => Array
        (
            [3] => hello
            [1] => world
        )
    [bar] => Array
        (
            [3] => Some other stuff
            [1] => Some more stuff 
        )
    [baz] => Array
        (
            [3] => value
        )
)

我怎么能在下面的表格中找到它:

Array
(
    [3] => Array
        (    
            [foo] => hello
            [bar] => Some other stuff           
            [baz] => value
        )
    [1] => Array
        (
            [foo] => world
            [bar] => Some more stuff  
        )
)

由于

1 个答案:

答案 0 :(得分:3)

$array;  //<--- assuming this is the array you are starting with

$new_array = array();  //<--- This is the new array you're building

foreach($array as $i=>$element)
{
    foreach($element as $j=>$sub_element)
    {
        $new_array[$j][$i] = $sub_element; //We are basically inverting the indexes
    }
}