2

I'm looking for solution to convert one dimensional array to multidimensional array. I have array:

array (size=10)
  'title' => string 'chart title' (length=11)
  'hAxis-title' => string 'hax title' (length=9)
  'hAxis-format' => string 'hax format' (length=10)
  'vAxis-title' => string 'vax title' (length=9)
  'vAxis-format' => string 'vax format' (length=10)
  'vAxis-gridlines-color' => string 'vax gridl color' (length=15)
  'vAxis-gridlines-count' => string '1' (length=1)
  'vAxis-gridlines-height-test' => string '2' (length=1)
  'height' => string '3' (length=1)
  'width' => string '4' (length=1)

and I need to convert it to:

array (size=5)
      'title' => string 'chart title' (length=11)
      'hAxis' => 
        array (size=2)
          'title' => string 'hax title' (length=9)
          'format' => string 'hax format' (length=10)
      'vAxis' => 
        array (size=2)
          'title' => string 'vax title' (length=9)
          'format' => string 'vax format' (length=10)
          'gridlines' =>
            array (size=3)
              'color' =>  string 'vax gridl color' (length=15)
              'count' =>  string '1' (length=1)
              'height' =>
                array (size=1)
                  'test' => string '2' (length=1)
      'height' => string '3' (length=1)
      'width' => string '4' (length=1)

I tried to use array functions, loops etc but only got to the point that I'm getting:

array (size=5)
      'title' => string 'chart title' (length=11)
      'hAxis' => 
        array (size=2)
          'title' => string 'hax title' (length=9)
          'format' => string 'hax format' (length=10)
      'vAxis' => 
        array (size=2)
          'title' => string 'vax title' (length=9)
          'format' => string 'vax format' (length=10)
      'height' => string '3' (length=1)
      'width' => string '4' (length=1)

Any ideas ? Please.

Strabek
  • 2,391
  • 3
  • 32
  • 39
  • 1
    @JayBlanchard really not.. Only the titles of the questions are the same. – Crembo Jun 03 '15 at 12:26
  • Unfortunately @Crembo a search reveals there are dozens of answers on SO for performing the transformation. – Jay Blanchard Jun 03 '15 at 12:28
  • Unfortunately @JayBlanchard they answer a really generic case. This case is very specific and you can see it depends on the key in the array. Some processing and logic on the key is required. – Crembo Jun 03 '15 at 12:30
  • @JayBlanchard I saw that one and many other but could not find solution and that's why I decided to create new question. – Strabek Jun 03 '15 at 12:32
  • @JayBlanchard thank you. Just found my answer here http://stackoverflow.com/questions/9628176/using-a-string-path-to-set-nested-array-data as you suggested – Strabek Jun 03 '15 at 12:45
  • You can explode the key, then loop over the exploded components and initialize an empty array for the component if it isn't the last component of the key and no such key exists for that step. – Sumurai8 Jun 03 '15 at 12:45
  • 1
    @Strabek I do not agree that this is a double. – splash58 Jun 03 '15 at 12:46
  • 1
    @splash58 maybe it's not exactly the same, but solution works for me – Strabek Jun 03 '15 at 12:48
  • 1
    Look there https://eval.in/374942 . Maybe it more interesting – splash58 Jun 03 '15 at 12:49
  • @splash58 thanks a lot – Strabek Jun 03 '15 at 12:52

0 Answers0