fork download
  1. <?php
  2.  
  3. $json = '{
  4. "foo.bar.test.thing": 1
  5. }';
  6.  
  7. $decoded = json_decode($json, true);
  8.  
  9.  
  10. $data = array();
  11. foreach ($decoded as $key => $value) {
  12. $keys = explode('.', $key);
  13. $data[] = buildNestedArray($keys, $value);
  14. }
  15.  
  16. print_r($data);
  17.  
  18. function buildNestedArray($keys, $value) {
  19. $new = array();
  20. foreach ($keys as $key) {
  21. if (empty($new)) {
  22. $new[$key] = $value;
  23. } else {
  24. array_walk_recursive($new, function(&$item) use ($key, $value) {
  25. if ($item === $value) {
  26. $item = array($key => $value);
  27. }
  28. });
  29. }
  30. }
  31.  
  32. return $new;
  33. }
Success #stdin #stdout 0.02s 52432KB
stdin
Standard input is empty
stdout
Array
(
    [0] => Array
        (
            [foo] => Array
                (
                    [bar] => Array
                        (
                            [test] => Array
                                (
                                    [thing] => 1
                                )

                        )

                )

        )

)