PHP:如何修复嵌套数组



我整晚都在与这个嵌套数组作斗争,所以我最终决定寻求一些帮助。我正在尝试将下面的这个嵌套数组更正为其他内容。请提供代码示例/解决方案。我不是最好的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:26am::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::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)
foreach ($array as &$oldArray) {
    $newArray = array();
    foreach ($oldArray as $subArray)
        $newArray += $subArray;
    $oldArray = $newArray;
}

这是在键不是每次都从零开始的情况下工作的(就像您的示例数组所显示的那样(。如果是,请使用array_merge

您可以尝试

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

实时演示

相关内容

  • 没有找到相关文章

最新更新