Multi Dimensional Array remaping

I have this array

array(4) {
  ["ID"]=>
  array(5) {
    [0]=>
    string(1) "0"
    [1]=>
    string(1) "1"
    [2]=>
    string(1) "2"
    [3]=>
    string(1) "3"
    [4]=>
    string(1) "4"
  }
  ["SECTION"]=>
  array(5) {
    [0]=>
    string(4) "ROOT"
    [1]=>
    string(4) "ROOT"
    [2]=>
    string(4) "ROOT"
    [3]=>
    string(4) "ROOT"
    [4]=>
    string(4) "ROOT"
  }
  ["PARENT"]=>
  array(5) {
    [0]=>
    string(4) "NONE"
    [1]=>
    string(4) "NONE"
    [2]=>
    string(4) "NONE"
    [3]=>
    string(5) "HONDA"
    [4]=>
    string(5) "HONDA"
  }
  ["NAME"]=>
  array(5) {
    [0]=>
    string(4) "NONE"
    [1]=>
    string(5) "MAZDA"
    [2]=>
    string(6) "NISSAN"
    [3]=>
    string(5) "HONDA"
    [4]=>
    string(6) "SUZUKI"
  }
}

it a result from a SQL query.

I want to transform it to be like this

array(){
        ["SECTION"] => 
        array(){
            ["ROOT"] => 
            array() =>
                ["NONE"] => 
                    array(0) =>
                        ["ID"] => 0
                        ["NAME"] => "NONE"
                    array(1) =>
                        ["ID"] => 1
                        ["NAME"] => "MAZDA"
                    array(0) =>
                        ["ID"] => 2
                        ["NAME"] => "NISSAN"
                    array(0) =>
                        ["ID"] => 3
                        ["NAME"] => "HONDA"
                    array(0) =>
                        ["ID"] => 4
                        ["NAME"] => "SUZUKI"
                ["HONDA"] => 
                    array(0) =>
                        ["ID"] => 0
                        ["NAME"] => "NONE"
                    array(1) =>
                        ["ID"] => 1
                        ["NAME"] => "MAZDA"
                    array(0) =>
                        ["ID"] => 2
                        ["NAME"] => "NISSAN"
                    array(0) =>
                        ["ID"] => 3
                        ["NAME"] => "HONDA"
                    array(0) =>
                        ["ID"] => 4
                        ["NAME"] => "SUZUKI"
        }

I have tried using php array_merge_recursive but it didn’t work as needed. Please if you have any light to the problem at hand I’ll be glad to see your answer.


Source: php

Leave a Reply