<?php


function array_to_pipe($array, $delimeter = '|', $parents = array(), $recursive = false)
{
    $result = '';

    foreach ($array as $key => $value) {
        $group = $parents;
        array_push($group, $key);

        // check if value is an array
        if (is_array($value)) {
            if ($merge = array_to_pipe($value, $delimeter, $group, true)) {
                $result = $result . $merge;
            }
            continue;
        }

        // check if parent is defined
        if (!empty($parents)) {
            $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) {
        $result = substr($result, 1);
    }

    return $result;
}

$list['item_lists'] = array(
	'single',
    'item1' => 'Item 1',
    'item2' => 'Item 2',
    'item3' => 'Item 3',
    'item4' => 'Item 4',
    'group' => array(
        'subgroup' => 'value',
        'nested' => array(
            'one' => 'two',
            'subnested' => array(
                'supernested' => 'supernested_value'
            )
        )
    ),
);

echo array_to_pipe($list['item_lists']);
