I have a string like:
$q = "menu=true&submenu=true&pcode=123456&code=123456";
I want to get the value of pcode = 123456 and code = 123456.
How can I get this?
asked Apr 26, 2011 at 9:04
saint
3,9155 gold badges22 silver badges17 bronze badges
-
Where do you get the string from?Felix Kling– Felix Kling2011年04月26日 09:09:28 +00:00Commented Apr 26, 2011 at 9:09
5 Answers 5
Use parse_str function if it's not from url (then use $_GET array)
Sign up to request clarification or add additional context in comments.
1 Comment
corsiKa
+1 Even though this doesn't exactly address the stated concern, it certainly seems to be valid advice. We don't want Bobby Tables dropping in for tea.
Use explode to get array from a string.
explode('&',$q);
It will explode string on every & character and return pieces in an array.
answered Apr 26, 2011 at 9:07
Headshota
21.4k13 gold badges63 silver badges82 bronze badges
Comments
See parse_str
answered Apr 26, 2011 at 9:08
Vincent Mimoun-Prat
28.7k16 gold badges85 silver badges126 bronze badges
Comments
$q ="menu=true&submenu=true&pcode=123456&code=123456" ;
// parse str values into an array called $pairs
parse_str($q, $pairs);
// loop through $pairs and display all values
foreach ($pairs as $key => $val) {
echo "$key => $val\n";
}
answered Apr 26, 2011 at 9:11
bumperbox
10.2k6 gold badges46 silver badges66 bronze badges
Comments
Please use php explode functions to do it
ex:
<?php
$q ="menu=true&submenu=true&pcode=123456&code=123456" ;
$pieces = explode("&",$q);
print_r($pieces);
foreach($pieces as $key=>$value){
$newVarible[]=explode("=",$value);
}
print_r($newVarible);
?>
answered Apr 26, 2011 at 9:07
Roshan Wijesena
3,1369 gold badges41 silver badges58 bronze badges
1 Comment
saint
this ll return an array of value pcode=123456 but i need pcode, ccode as keys and 123456, 123456 as values...! :(
Explore related questions
See similar questions with these tags.
lang-php