fork download
  1. <?php
  2.  
  3. $data = array(
  4. "foo" => array(
  5. "bar" => array(
  6. "baz" => "hello",
  7. "bee" => "welcome"
  8. ),
  9. "byte" => "yup",
  10. "sigmund" => array( ),
  11. ),
  12. "tale" => "wup"
  13. );
  14.  
  15. function toPathArray($nestedArr) {
  16. $res = array();
  17. foreach ($nestedArr as $key => $val) {
  18. if (is_array($val)) {
  19. foreach (toPathArray($val) as $innerKey => $innerVal) {
  20. $res["$key/$innerKey"] = $innerVal;
  21. }
  22. } else {
  23. $res[$key] = $val;
  24. }
  25. }
  26. return $res;
  27. }
  28.  
  29. print_r(toPathArray($data));
  30.  
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
Array
(
    [foo/bar/baz] => hello
    [foo/bar/bee] => welcome
    [foo/byte] => yup
    [tale] => wup
)