twitter
    Find out what I'm doing, Follow Me :)
If you have any question, suggestion or article topic for me to write, feel free to contact me through my shout box. ;) Some time I need an idea to write. hehe Hopefully I can help you and share my expertise.

php parse string as variable

Yesterday I blog about variable variables . By adding double sign dollar $$ we can make a string as a variable.What going to happen if I have set of variable in one string?. I got this problem when I'm using jQuery nested sortable plugin in my daily coding. yeah, nestedsortable plugin have a serialize function to get all element. I can use jQuery post or get to send this paremeter to process using ajax but I don't want to do that because I need to do other entry first before sending all variables at one time.

my problem is, the serialize hash format is like this
$str = "list_container[0][id]=1&list_container[1][id]=2
&list_container[1][children][0][id]=3
&list_container[1][children][0][children][0][id]=4
&list_container[1][children][0][children][1][id]=5";
at first I'm thinking to use explode function to split a string by using ampersand '&' and = as a delimiter and variable variables to get a variable but I think it's not so effective. Then I ask uncle google what others people do to solve this problem.Yeah, I like google... one of the search result is by using parse_str. It's so easy and this is what I want.

How to use parse_str?.
$str = "list_container[0][id]=1&list_container[1][id]=2
&list_container[1][children][0][id]=3
&list_container[1][children][0][children][0][id]=4
&list_container[1][children][0][children][1][id]=5";
parse_str($str);

//Now parse_str function will return string value as an array set.
//I can get the value by using $list_container as a variable
var_dump($list_container);
You can download sample file :How to use parse_str in form post

0 comments:

Post a Comment