0

I have been struggling with this nested array all night, so I finally decided to ask for some help on this. I am trying to correct this nested array I have below into something else. Please respond with code examples/solutions. I am not the best PHP programmer. Thank you all in advance!

What I want it to look like:

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)

What it currently looks like:

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)

Update 4/24 10:26am:: bwoebi's Example Output This is good thank you, but not 100% what I need. it removed the nested arrays but also separated all the keys again. I need the first 3 keys in one "group" like my original example shows.

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)

Update 4/24 10:36am:: bwoebi's Corrected Example Output

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 Answers 2

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

This works under the condition that your keys don't begin every time at zero (like your example array shows it). If they do, use array_merge.

Sign up to request clarification or add additional context in comments.

Comments

0

You can try

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

print_r($array);

Output

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

3 Comments

I know, you love array_map really, but here it won't show high performance and it's more confusing than helpful for such a simple use case.
Your code does not work actually ... eval.in/17458 on the performance that is micro optimization
sorry, I misread the answer; updated my answer. Yep, now it's microoptimization. But before my code was O(n) and yours is O(n*m) ... ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.