функция stringify на php
function stringify($value, $replacer = ' ', $spaceCount = 1)
{
$result = [];
if (!is_array($value)) {
return toString($value);
}
return iter($value, $replacer, $spaceCount);
}
function iter($value, $replacer, $spaceCount, $depth = 0) {
$placeHolder = str_repeat($replacer, $spaceCount + $depth);
foreach ($value as $k => $v) {
$strV = toString($v);
if (!is_array($v)) {
$result[] = "{$placeHolder}{$k}: {$strV}" ;
} else {
$result[] = "{$placeHolder}{$k}: ". iter($v, $replacer, $spaceCount, $depth + $spaceCount);
}
}
$result = join("\n",$result);
return "{\n{$result}\n{$placeHolder}}";
}
Проблема в том что должно получиться это:
{
|-hello: world
|-is: true
|-nested: {
|-|-count: 5
|-}
}
А получается вот это:
{
|-hello: world
|-is: true
|-nested: {
|-|-count: 5
|-|-}
|-}
на вот таких данных
$data = [
'hello' => 'world',
'is' => true,
'nested' => ['count' => 5],
];