拆分嵌套数组用于php

时间:2017-07-12 06:23:50

标签: php arrays split nested

我有一个数组,想要拆分它们。可能是两个,树或更多

array(
     name=>array(
                0=>asda.jpg,
                1=>kewj.jpg
                ),
     type=>array(
                0=>jpg,
                1=>jpg
                ),
     size=>array(
                0=>2133,
                1=>2222
                )
     )

我希望拆分为两个数组或更多

array(
     name=>asd.jpg,
     type=>jpg,
     size=>2133
     )


array(
       name=>kewj.jpg,
       type=>jpg,
       size=>2222
      )

2 个答案:

答案 0 :(得分:3)

在这里你可以像这样实现它

示例

<?php
$arr = array(
'name' => array(
          0 => 'asda.jpg',
          1 => 'kewj.jpg'
        ),
'type' => array(
          0 => 'jpg',
          1 => 'jpg'
        ),
'size' => array(
          0 => '2133',
          1 => '2222'
        )
);
$arraySplit = array();
foreach($arr as $key => $value) {
    foreach($value as $key2 => $value2) {
            $arraySplit[$key2][$key] = $value2;
    }
}
echo "<pre>";
print_r($arraySplit);

<强>输出

Array
(
[0] => Array
    (
        [name] => asda.jpg
        [type] => jpg
        [size] => 2133
    )

[1] => Array
    (
        [name] => kewj.jpg
        [type] => jpg
        [size] => 2222
    )

)

答案 1 :(得分:1)

我认为这应该是解决方案:

foreach ($array1 as $value) {

    for($node=0;$node<count($value);$node++){

        $arr[$node][] = $value[$node];
    }    
}

谢谢!