1

I have a string

$str = "recordsArray[]=3&recordsArray[]=1";

Is there any simple way to make this to an array other than exploding this string at &? Im expecting a result like this print_r($str);

//expecting output
array('0' => '3','1' => '1');
3
  • 1
    Looks more like a regular expression would work better then unserializing. There's no content that looks to be serialized in your question. Commented Feb 3, 2012 at 5:26
  • You could also use parse_str() Commented Feb 3, 2012 at 5:28
  • possible duplicate of How can I split a string? Commented Feb 3, 2012 at 5:32

2 Answers 2

7

Yes there is: parse_strDocs; Example (Demo):

parse_str("recordsArray[]=3&recordsArray[]=1", $test);
print_r($test);
Sign up to request clarification or add additional context in comments.

1 Comment

parse_str("recordsArray[]=3&recordsArray[]=1",$str); print_r($str);exit; //Working :)
2

Use preg_match_all like this:

$str = "recordsArray[]=3&recordsArray[]=1";
if ( preg_match_all('~\[\]=(\d+)~i', $str, $m) )
   print_r ( $m[1] );

OUTPUT:

Array
(
    [0] => 3
    [1] => 1
)

1 Comment

+1 Thank you it worked ! ..but i prefer alex's answer..its simple

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.