<?php
function array_to_pipe
($array, $delimeter = '|', $parents = array(), $recursive = false) {
$result = '';
foreach ($array as $key => $value) {
$group = $parents;
// check if value is an array
if ($merge = array_to_pipe($value, $delimeter, $group, true)) {
$result = $result . $merge;
}
continue;
}
// check if parent is defined
$result = $result . PHP_EOL
. implode($delimeter, $group) . $delimeter . $value; continue;
}
$result = $result . PHP_EOL . $key . $delimeter . $value;
}
// somehow the function outputs a new line at the beginning, we fix that
// by removing the first new line character
if (!$recursive) {
}
return $result;
}
$list['item_lists'] = array( 'single',
'item1' => 'Item 1',
'item2' => 'Item 2',
'item3' => 'Item 3',
'item4' => 'Item 4',
'subgroup' => 'value',
'one' => 'two',
'supernested' => 'supernested_value'
)
)
),
);
echo array_to_pipe($list['item_lists']);
PD9waHAKCgpmdW5jdGlvbiBhcnJheV90b19waXBlKCRhcnJheSwgJGRlbGltZXRlciA9ICd8JywgJHBhcmVudHMgPSBhcnJheSgpLCAkcmVjdXJzaXZlID0gZmFsc2UpCnsKICAgICRyZXN1bHQgPSAnJzsKCiAgICBmb3JlYWNoICgkYXJyYXkgYXMgJGtleSA9PiAkdmFsdWUpIHsKICAgICAgICAkZ3JvdXAgPSAkcGFyZW50czsKICAgICAgICBhcnJheV9wdXNoKCRncm91cCwgJGtleSk7CgogICAgICAgIC8vIGNoZWNrIGlmIHZhbHVlIGlzIGFuIGFycmF5CiAgICAgICAgaWYgKGlzX2FycmF5KCR2YWx1ZSkpIHsKICAgICAgICAgICAgaWYgKCRtZXJnZSA9IGFycmF5X3RvX3BpcGUoJHZhbHVlLCAkZGVsaW1ldGVyLCAkZ3JvdXAsIHRydWUpKSB7CiAgICAgICAgICAgICAgICAkcmVzdWx0ID0gJHJlc3VsdCAuICRtZXJnZTsKICAgICAgICAgICAgfQogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICB9CgogICAgICAgIC8vIGNoZWNrIGlmIHBhcmVudCBpcyBkZWZpbmVkCiAgICAgICAgaWYgKCFlbXB0eSgkcGFyZW50cykp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