fork(8) download
  1. <?php
  2.  
  3.  
  4. function array_to_pipe($array, $delimeter = '|', $parents = array(), $recursive = false)
  5. {
  6. $result = '';
  7.  
  8. foreach ($array as $key => $value) {
  9. $group = $parents;
  10. array_push($group, $key);
  11.  
  12. // check if value is an array
  13. if (is_array($value)) {
  14. if ($merge = array_to_pipe($value, $delimeter, $group, true)) {
  15. $result = $result . $merge;
  16. }
  17. continue;
  18. }
  19.  
  20. // check if parent is defined
  21. if (!empty($parents)) {
  22. $result = $result . PHP_EOL . implode($delimeter, $group) . $delimeter . $value;
  23. continue;
  24. }
  25.  
  26. $result = $result . PHP_EOL . $key . $delimeter . $value;
  27. }
  28.  
  29. // somehow the function outputs a new line at the beginning, we fix that
  30. // by removing the first new line character
  31. if (!$recursive) {
  32. $result = substr($result, 1);
  33. }
  34.  
  35. return $result;
  36. }
  37.  
  38. $list['item_lists'] = array(
  39. 'single',
  40. 'item1' => 'Item 1',
  41. 'item2' => 'Item 2',
  42. 'item3' => 'Item 3',
  43. 'item4' => 'Item 4',
  44. 'group' => array(
  45. 'subgroup' => 'value',
  46. 'nested' => array(
  47. 'one' => 'two',
  48. 'subnested' => array(
  49. 'supernested' => 'supernested_value'
  50. )
  51. )
  52. ),
  53. );
  54.  
  55. echo array_to_pipe($list['item_lists']);
  56.  
Success #stdin #stdout 0.03s 52480KB
stdin
Standard input is empty
stdout
0|single
item1|Item 1
item2|Item 2
item3|Item 3
item4|Item 4
group|subgroup|value
group|nested|one|two
group|nested|subnested|supernested|supernested_value