Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Does it solves your problem? Use <code>$inputString</code> to put your real string.</p> <pre><code> &lt;?php // ------------------------------------------------------ your input goes here ------------------------------------------------------ $inputString = 'Brazil|SaoPaulo|Diadema|RuadaFe Brazil|SaoPaulo|Diadema|RuadoLimoeiro Brazil|SaoPaulo|SaoCaetano|RuadasLaranjeiras Brazil|Parana|Curitiba|ComendadorAraujo USA|NewJersey|JerseyCity|WhashingtonBoulervard USA|NewJersey|JerseyCity|RiverCourt'; class item { public $name = null; public function getChildrenByName($strName) { $ret = null; # this variable should be defined in interface, but i skipped it so it wont be printed in json when obj does not have childrens if( !isset( $this-&gt;children ) ) { $this-&gt;children = array( ); } foreach ( $this-&gt;children as $child ) { if( $child-&gt;name === $strName ) { $ret = $child; break; } } if ( !$ret ) { $this-&gt;children[] = self::spawnByName( $strName ); } return $this-&gt;children[ count($this-&gt;children) - 1]; } static public function spawnByName($strName) { $ret = new item(); $ret-&gt;name = $strName; return $ret; } } class listManager { protected $list = array(); public function getList() { return $this-&gt;list; } public function addPath( $desiredPath ) { # path needs to be as array if ( is_string( $desiredPath ) ) { $desiredPath = explode('|', $desiredPath); } # create root element if it does not already exists if ( !isset( $this-&gt;list[$desiredPath[0]] ) ) { $this-&gt;list[$desiredPath[0]] = item::spawnByName($desiredPath[0]); } $curElement = $this-&gt;list[$desiredPath[0]]; for( $i=1; $i&lt;count($desiredPath); $i++ ) { $curElement = $curElement-&gt;getChildrenByName( $desiredPath[$i] ); } } protected function spawnElement( $strName ) { $ret = new item(); $ret-&gt;name = $strName; return $ret; } } $output = array(); $expl = explode(' ', $inputString); $list = new listManager(); foreach ( $expl as $key =&gt; $path ) { $list-&gt;addPath( $path ); } $output = ''; foreach ( $list-&gt;getList() as $singleVariable ) { $output .= json_encode($singleVariable, JSON_PRETTY_PRINT) . ",\n"; } echo '&lt;pre&gt;'.$output.'&lt;/pre&gt;'; ?&gt; </code></pre> <p>Above code produces following json out of your sample code:</p> <pre><code>{ "name": "Brazil", "children": [{ "name": "SaoPaulo", "children": [{ "name": "Diadema", "children": [{ "name": "RuadaFe" } ] } ] }, { "name": "SaoPaulo", "children": [{ "name": "Diadema", "children": [{ "name": "RuadoLimoeiro" } ] } ] }, { "name": "SaoPaulo", "children": [{ "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": "NewJersey", "children": [{ "name": "JerseyCity", "children": [{ "name": "RiverCourt" } ] } ] } ] } </code></pre> <p><strong>edit:</strong> changed, does it fit now?</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