3

I have an input array like this one, where "axis", "maximum_deviation" and "current" are table columns. I need to insert 4 rows:

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

I need to turn it to this format:

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

All rows are of variable length.

1 Answer 1

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;
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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.