PHP Built Multidimensional Array after explode function -
i try found function build multidimensional array after explode.
for example : here create array explode.
$str = 'root/foo/bar/file.jpg'; $ar = explode('/', $str);
$ar => array('root', 'foo' , 'bar', 'file.jpg');
then want ouput :
array(3) { ['root']=> ['foo']=> ['bar']=> "file.jpg" }
any idea ?
thx
here 1 way problem can approached.
<?php $str = 'root/foo/bar/file.jpg'; $parts = explode("/", $str); $leaf = array_pop($parts); $tree = array(); $branch = &$tree; foreach($parts $v){ $branch[$v] = array(); $branch = &$branch[$v]; } $branch = $leaf; print_r($tree);
try here
Comments
Post a Comment