输入数组转换

时间:2011-09-04 18:21:15

标签: php arrays forms

我有一个像这样的输入数组,其中“axis”,“maximum_deviation”和“current”是表列。我需要插入4行:

Array
(
    [axis_deviation] => Array
        (
            [0] => Array
                (
                    [axis] => Array
                        (
                            [0] => axis 1
                            [1] => axis 2
                            [2] => axis 3
                            [3] => axis 4
                        )

                )

            [1] => Array
                (
                    [maximum_deviation] => Array
                        (
                            [0] => 0.001
                            [1] => 0.003
                            [2] => 12
                            [3] => 0.003
                        )

                )

            [2] => Array
                (
                    [current] => Array
                        (
                            [0] => 0.002
                            [1] => 0.002
                            [2] => 13
                            [3] => 0.003
                        )
                )
        )
)

我需要把它变成这种格式:

Array
(
    [0] => Array
        (
            [axis] => axis 1
            [maximum_deviation] => 0.001
            [current] => 0.002
        )

    [1] => Array
        (
            [axis] => axis 2
            [maximum_deviation] => 0.001
            [current] => 0.002
        )

    [2] => Array
        (
            [axis] => axis 3
            [maximum_deviation] => 0.001
            [current] => 0.002
        )

    [2] => Array
        (
            [axis] => axis 4
            [maximum_deviation] => 0.001
            [current] => 0.002
        )
)

所有行都是可变长度。

1 个答案:

答案 0 :(得分:5)

$result = array();
foreach ($array['axis_deviation'] as $foo) {
  foreach ($foo as $key => $bar) {
    foreach ($bar as $index => $value) {
      if (!array_key_exists($index, $result)) $result[$index] = array();
      if (!array_key_exists($key, $result[$index])) $result[$index][$key] = array();
      $result[$index][$key] = $value;
    }
  }
}