Anyone got any idea on how to deal with explode to miss elements if there nesting inside something? Examples below.
So i have a data structure like so...
{1,2,3,{4,2},5,6}
I am trying to convert that into a php array like so...
array( 1, 2, 3, array( 4, 2 ), 5, 6);
Any ideas on how that might work? Some suggestions have been Regex or explode while monitoring a state machine? ( but i have no idea on how they work :S )
You can try
$string = "{1,2,{3,2},4,5}" ;
var_dump(__array($string));
$string = "[1,2,3,[4,2],5]" ;
var_dump(__array($string));
$string = "(1,2,(3,2),4,5)" ;
var_dump(__array($string,array("(",")")));
$string = "+1,2,+3,2-,4,5-" ;
var_dump(__array($string,array("+","-")));
function __array($string,$seperator = array("{","}"))
{
$string = str_replace($seperator, array("[","]"), $string);
return json_decode($string);
}
Output
array
0 => int 1
1 => int 2
2 =>
array
0 => int 3
1 => int 2
3 => int 4
4 => int 5