coolguy
coolguy

Reputation: 7954

Parse a string to array

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');

Upvotes: 1

Views: 176

Answers (2)

alex
alex

Reputation: 580

Yes there is: parse_strDocs; Example (Demo):

parse_str("recordsArray[]=3&recordsArray[]=1", $test);
print_r($test);

Upvotes: 7

anubhava
anubhava

Reputation: 786329

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
)

Upvotes: 2

Related Questions