0

我整晚都在为这个嵌套数组苦苦挣扎,所以我最终决定在这方面寻求一些帮助。我正在尝试将下面的嵌套数组更正为其他内容。请回复代码示例/解决方案。我不是最好的 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:: 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)
4

2 回答 2

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

这在您的密钥并非每次都从零开始的情况下有效(如您的示例数组所示)。如果他们这样做,请使用array_merge.

于 2013-04-24T14:13:18.913 回答
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
        )

)

现场演示

于 2013-04-24T14:18:18.547 回答