Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP - Nested array keys based on string lines
    primarykey
    data
    text
    <p>I need a help. I'm looking for a solution for this problem! I have a file which contains the following pattern:</p> <blockquote> <p>Brazil|SaoPaulo|Diadema|RuadaFe Brazil|SaoPaulo|Diadema|RuadoLimoeiro Brazil|SaoPaulo|SaoCaetano|RuadasLaranjeiras Brazil|Parana|Curitiba|ComendadorAraujo USA|NewJersey|JerseyCity|WhashingtonBoulervard USA|NewJersey|JerseyCity|RiverCourt</p> </blockquote> <p>Which should bring after some array key implementation, something like this (after apply json_encode call on php):</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": "WhashingonBoulevard"}, {"name": "RiverCourt"} ] } ] } ] } ] </code></pre> <p>And keep going and going (and even more deeper too). Please, help me team... thanks in advance.</p> <p>Here what I get until now:</p> <p>Array ( [Brazil] => Array ( [SaoPaulo] => Array ( [Diadema] => Array ( [RuadoLimoeiro] => )</p> <pre><code> [SaoCaetano] =&gt; Array ( [RuadasLaranjeiras] =&gt; ) ) [Parana] =&gt; Array ( [Curitiba] =&gt; Array ( [ComendadorAraujo] =&gt; ) ) ) [USA] =&gt; Array ( [NewJersey] =&gt; Array ( [JerseyCity] =&gt; Array ( [WhashingtonBoulervard] =&gt; [RiverCourt] =&gt; ) ) ) </code></pre> <p>)</p> <p>And here is the json encoded:</p> <pre><code>{ "Brazil":{ "SaoPaulo": {"Diadema": {"RuadoLimoeiro":null}, "SaoCaetano":{"RuadasLaranjeiras":null} }, "Parana": {"Curitiba": {"ComendadorAraujo":null} } }, "USA":{ "NewJersey":{ "JerseyCity":{ "WhashingtonBoulervard":null, "RiverCourt":null} } } } </code></pre> <p>As you can see, the "names" and "child" is missing because is not an array key structure, also something is wrong, because I'm missing some values on SaoPaulo.</p> <p>Here is the function:</p> <pre><code>foreach($strings as $string) { $parts = array_filter(explode('|', $string)); $ref = &amp;$result; foreach($parts as $p) { // echo $p; if(!isset($ref[$p])) { $ref[$p] = array(); // $ref[$p] = array("name"=&gt;$p); } $ref = &amp;$ref[$p]; } $ref = null; } </code></pre> <p>-------------------------------- AFTER SOME ANSWERS --------------------------</p> <pre><code>{ "name": "Brazil(country)", "children": [ { "name": "SaoPaulo(state)", // only one state "children": [ { "name": "Diadema(city)", // only one city "children": [ {"name": "RuadaFe(street)"}, // two streets under the same city... {"name": "RuadoLimoeiro(street)"} ] }, { "name": "SaoCaetano(city)", "children": [ {"name": "RuadasLaranjeiras(street)"} ] }, ] "name": "Parana(state)", "children": [ { "name": "Curitiba(city)", "children": [ {"name": "ComendadorAraujo(street)"} ] } ] },... </code></pre> <p>I put on parentesis the structure (country, state, city, street) just to clarify what i want. Got it?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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