array(4) { ["key1"]=> string(6) "Item 1" ["key2"]=> string(6) "Item 2" ["key3"]=" /> array(4) { ["key1"]=> string(6) "Item 1" ["key2"]=> string(6) "Item 2" ["key3"]=" /> array(4) { ["key1"]=> string(6) "Item 1" ["key2"]=> string(6) "Item 2" ["key3"]="/>

Output of a multidimensional array as a grouped key=>value list

25 Views Asked by At

Given is a multidimensional array like this:

array(51) {
  ["ACTION"]=>
  array(4) {
    ["key1"]=>
    string(6) "Item 1"
    ["key2"]=>
    string(6) "Item 2"
    ["key3"]=>
    string(6) "Item 3"
    ["key4"]=>
    string(6) "Item 4"
  }
 ["ADMIN"]=>
  array(2) {
    ["key1"]=>
    string(6) "Item 1"
    ["key2"]=>
    array(2) {
      ["subkey1"]=>
      string(6) "Item 1"
      ["subkey2"]=>
      string(6) "Item 2"
    }
  }

I would like to output this array as a grouped list of key/value pairs like this:

[ACTION]
key1=Item 1
key2=Item 2
key3=Item 3
key4=Item 4

[ADMIN]
key1=Item 1

[ADMIN.key2]
subkey1=Item 1
subkey2=Item 2

I tried many ways but can't get the grouping to work. Any help is highly appreciated.

For example: I tried to "foreach" through the array, output the key and then key pairs values in case the value was a string. But as the array is sorted alphabetically (key), there could be a 3D array followed by a 2D entry and the [HEADER] do not relate to the correct group.

0

There are 0 best solutions below