0

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?

mickmackusa
49.3k13 gold badges98 silver badges165 bronze badges
asked Apr 26, 2011 at 9:04
1
  • Where do you get the string from? Commented Apr 26, 2011 at 9:09

5 Answers 5

4

Use parse_str function if it's not from url (then use $_GET array)

https://www.php.net/manual/en/function.parse-str.php

answered Apr 26, 2011 at 9:07
Sign up to request clarification or add additional context in comments.

1 Comment

+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.
3

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

Comments

1

See parse_str

answered Apr 26, 2011 at 9:08

Comments

0
 $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

Comments

0

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

1 Comment

this ll return an array of value pcode=123456 but i need pcode, ccode as keys and 123456, 123456 as values...! :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.