如何重构多维数组?

时间:2012-02-10 15:07:32

标签: php multidimensional-array

基本上我的数组有问题,这就是我所拥有的:

$test = 
array(
  "image" => array(
        "name" => array(
            "name.jpg",
            "name2.jpg",
            "name3.jpg"
        ),
        "type"  => array(
                "image/jpeg",
                "image/jpeg",
                "image/jpeg",
        ),
        "size" => array(
          118108,
          589788,
          1387074
        )
    )
)

这就是我想要的:

$test2 =
array(
    "0" => array(
        "name" => "name.jpg",
        "type" => "image/jpeg",
        "size" => 118108
    ),
    "1" => array(
        "name" => "name2.jpg",
        "type" => "image/jpeg",
        "size" => 589788
    ),
    "2" => array(
        "name" => "name3.jpg",
        "type" => "image/jpeg",
        "size" => 1387074
    )

)

所以我需要某种脚本将第一个数组结构重做到第二个

4 个答案:

答案 0 :(得分:2)

这是一个更通用的解决方案。它将使用任何属性名称重建数组。我还将它包装在一个函数中,以便于使用。

使用:

function func(array $input)
{
    $result = array();

    foreach ($input as $type => $properties) {
        foreach ($properties as $name => $values) {
            for ($i = 0; $i < count($values); $i++) {
                $result[$i][$name] = $values[$i];
            }
        }
    }

    return $result;
}

结果:

Array
(
    [0] => Array
        (
            [name] => name.jpg
            [type] => image/jpeg
            [size] => 118108
        )

    [1] => Array
        (
            [name] => name2.jpg
            [type] => image/jpeg
            [size] => 589788
        )

    [2] => Array
        (
            [name] => name3.jpg
            [type] => image/jpeg
            [size] => 1387074
        )

)

希望它有所帮助!

答案 1 :(得分:1)

我认为这样做:

<?php

$result = array();

foreach($test['image'] as $key=>$data){
foreach($data as $id=>$val){
    $result[$id][$key] = $val;
}
}

?>

答案 2 :(得分:1)

这应该这样做:

$test = 
array(
  "image" => array(
        "name" => array(
            "name.jpg",
            "name2.jpg",
            "name3.jpg"
        ),
        "type"  => array(
                "image/jpeg",
                "image/jpeg",
                "image/jpeg",
        ),
        "size" => array(
          118108,
          589788,
          1387074
        )
    )
);

$output=array();

foreach(range(0,(count($test["image"])-1)) as $i)
{
  $output[$i]=array($test["image"]["name"][$i],$test["image"]["type"][$i],$test["image"]["size"][$i]);
}

var_dump($output);

答案 3 :(得分:1)

PHP语法允许使用多种方法,例如foreach循环(对于项目,键总是相同的):

$output = array();
foreach( $test["image"]["name"] as $key => $name){
  $output[] = array(
    'name' => $name,
    'type' => $test["image"]["type"][$key],
    'size' => $test["image"]["size"][$key],
  );
}

或者在条件允许的情况下使用array_shift / array_pop“看起来不好”:

while(
  ($name = array_pop( $test["image"]["name"])) &&
  ($type = array_pop( $test["image"]["type"])) &&
  ($size = array_pop( $test["image"]["size"]))
) {
    $output[] = array(
      'name' => $name,
      'type' => $type,
      'size' => $size,
    );
}

第二个会破坏输入数组,它比第一个更难阅读,但它确保没有现有数组键的问题。