PHP:如何修复嵌套数组

时间:2013-04-24 14:10:40

标签: php arrays multidimensional-array

我整晚都在努力使用这个嵌套阵列,所以我最终决定在这方面寻求帮助。我正在尝试将下面的嵌套数组更正为其他内容。请回复代码示例/解决方案。我不是最好的PHP程序员。提前谢谢大家!

我希望它看起来像是什么:

array
  0 => 
      'enginename' => string 'Engine1' (length=19)
      'status' => string 'up' (length=2)
      'time' => string '2013-04-17 09:28:00' (length=19)
  1 => 
      'enginename' => string 'Engine2' (length=19)
      'status' => string 'up' (length=2)
      'time' => string '2013-04-17 09:28:00' (length=19)

目前的情况:

Writing data... 
The total # of columns in this query are: 3 
The length of the first array is: 363 
array
  0 => 
    array
      0 => 
        array
          'enginename' => string 'Engine1' (length=19)
      1 => 
        array
          'status' => string 'up' (length=2)
      2 => 
        array
          'time' => string '2013-04-17 09:28:00' (length=19)
  1 => 
    array
      3 => 
        array
          'enginename' => string 'Engine2' (length=20)
      4 => 
        array
          'status' => string 'up' (length=2)
      5 => 
        array
          'time' => string '2013-04-17 09:28:00' (length=19)
  2 => 
    array
      6 => 
        array
          'enginename' => string 'Engine3' (length=30)
      7 => 
        array
          'status' => string 'up' (length=2)
      8 => 
        array
          'time' => string '2013-04-17 09:28:00' (length=19)

更新4/24 10:26 am :: bwoebi的示例输出 这很好,谢谢你,但不是100%我需要的。它删除了嵌套数组,但又重新分隔了所有键。我需要一个“组”中的前3个键,就像我原来的示例所示。

array
  0 => 
    array
      'enginename' => string 'Engine1' (length=19)
  1 => 
    array
      'status' => string 'up' (length=2)
  2 => 
    array
      'time' => string '2013-04-17 09:28:00' (length=19)
  3 => 
    array
      'enginename' => string 'Engine2' (length=20)
  4 => 
    array
      'status' => string 'up' (length=2)
  5 => 
    array
      'time' => string '2013-04-17 09:28:00' (length=19)
  6 => 
    array
      'enginename' => string 'Engine3' (length=30)
  7 => 
    array
      'status' => string 'up' (length=2)
  8 => 
    array
      'time' => string '2013-04-17 09:28:00' (length=19)

更新4/24 10:36 am :: bwoebi的更正示例输出

array
  0 => 
    array
      'enginename' => string 'Engine1' (length=19)
      'status' => string 'up' (length=2)
      'time' => string '2013-04-17 09:28:00' (length=19)
  1 => 
    array
      'enginename' => string 'Engine2' (length=20)
      'status' => string 'up' (length=2)
      'time' => string '2013-04-17 09:28:00' (length=19)
  2 => 
    array
      'enginename' => string 'Engine3' (length=30)
      'status' => string 'up' (length=2)
      'time' => string '2013-04-17 09:28:00' (length=19)

2 个答案:

答案 0 :(得分:2)

foreach ($array as &$oldArray) {
    $newArray = array();
    foreach ($oldArray as $subArray)
        $newArray += $subArray;
    $oldArray = $newArray;
}

这适用于每次都没有从零开始键的情况(就像你的示例数组所示)。如果是,请使用array_merge

答案 1 :(得分:0)

你可以尝试

$array = array_map(function ($v) {
    return array_map("current", $v);
}, $array);

print_r($array);

输出

Array
(
    [0] => Array
        (
            [0] => Engine1
            [1] => up
            [2] => 2013-04-17 09:28:00
        )

    [1] => Array
        (
            [0] => Engine2
            [1] => up
            [2] => 2013-04-17 09:28:00
        )

    [2] => Array
        (
            [0] => Engine3
            [1] => up
            [2] => 2013-04-17 09:28:00
        )

)

Live Demo