繁体   English   中英

PHP数组更改数组结构

[英]PHP array change array structure

我有以下形式的数组,

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  
        )
)

谢谢

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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