Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is not outright trivial, but what I did is actually the same as you (the same way as in your edited question), but I also keep track of those arrays to rename the keys. And that's what I do afterwards then:</p> <pre><code>$separator = [' ', '|']; $buffer = file_get_contents($file); $entries = explode($separator[0], $buffer); $result = []; $named[] = &amp;$result; ######## foreach ($entries as $entry) { $each = explode($separator[1], $entry); $pointer = &amp; $result; while ($current = array_shift($each)) { if (!isset($pointer[$current])) { unset($children); $children = []; $named[] = &amp;$children; ######## $pointer[$current] = ['name' =&gt; $current, 'children' =&gt; &amp;$children]; } $pointer = &amp; $pointer[$current]['children']; } } foreach($named as $offset =&gt; $namedArray) { $keys = array_keys($namedArray); foreach($keys as $key) { $named[$offset][] = &amp;$namedArray[$key]; unset($named[$offset][$key]); } } print_r($result); </code></pre> <p><a href="http://eval.in/18420" rel="nofollow">See it in action.</a></p> <p>You probably <em>might</em> want to modify this by checking in the later foreach if children are empty (leaf-nodes) and then removing the children entry as well.</p> <p>Here the modified foreach at the end and json output:</p> <pre><code>foreach($named as $offset =&gt; $namedArray) { $keys = array_keys($namedArray); foreach($keys as $key) { if (!$namedArray[$key]['children']) { unset($namedArray[$key]['children']); } $named[$offset][] = &amp;$namedArray[$key]; unset($named[$offset][$key]); } } echo json_encode($result, JSON_PRETTY_PRINT); </code></pre> <p>Output:</p> <pre><code>[ { "name": "Brazil", "children": [ { "name": "SaoPaulo", "children": [ { "name": "Diadema", "children": [ { "name": "RuadaFe" }, { "name": "RuadoLimoeiro" } ] }, { "name": "SaoCaetano", "children": [ { "name": "RuadasLaranjeiras" } ] } ] }, { "name": "Parana", "children": [ { "name": "Curitiba", "children": [ { "name": "ComendadorAraujo" } ] } ] } ] }, { "name": "USA", "children": [ { "name": "NewJersey", "children": [ { "name": "JerseyCity", "children": [ { "name": "WhashingtonBoulervard" }, { "name": "RiverCourt" } ] } ] } ] } ] </code></pre> <p>The full code-example at a glance:</p> <pre><code>&lt;?php /** * PHP - Nested array keys based on string lines * @link http://stackoverflow.com/a/16305236/2261774 */ $file = 'data://text/plain;base64,' . base64_encode('Brazil|SaoPaulo|Diadema|RuadaFe Brazil|SaoPaulo|Diadema|RuadoLimoeiro Brazil|SaoPaulo|SaoCaetano|RuadasLaranjeiras Brazil|Parana|Curitiba|ComendadorAraujo USA|NewJersey|JerseyCity|WhashingtonBoulervard USA|NewJersey|JerseyCity|RiverCourt'); $separator = [' ', '|']; $buffer = file_get_contents($file); $entries = explode($separator[0], $buffer); $result = []; $named[] = &amp;$result; foreach ($entries as $entry) { $each = explode($separator[1], $entry); $pointer = &amp; $result; while ($current = array_shift($each)) { if (!isset($pointer[$current])) { unset($children); $children = []; $named[] = &amp;$children; $pointer[$current] = ['name' =&gt; $current, 'children' =&gt; &amp;$children]; } $pointer = &amp; $pointer[$current]['children']; } } unset($pointer); foreach($named as $offset =&gt; $namedArray) { $keys = array_keys($namedArray); foreach($keys as $key) { if (!$namedArray[$key]['children']) { unset($namedArray[$key]['children']); } $named[$offset][] = &amp;$namedArray[$key]; unset($named[$offset][$key]); } } unset($named); echo json_encode($result, JSON_PRETTY_PRINT); </code></pre> <hr> <p>your question JSON:</p> <pre><code>{ "name": "Brazil", "children": [ { "name": "SaoPaulo", "children": [ { "name": "Diadema", "children": [ {"name": "RuadaFe"}, {"name": "RuadoLimoeiro"} ] }, </code></pre> <p>My Answer JSON:</p> <pre><code>[ { "name": "Brazil", "children": [ { "name": "SaoPaulo", "children": [ { "name": "Diadema", "children": [ { "name": "RuadaFe" }, { "name": "RuadoLimoeiro" } ] }, </code></pre> <p>The only difference I can spot is that the root-nodes are wrapped inside an array in my answer, but it should be trivial to fetch them out there if that really is your issue ;)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload