Reputation: 7954
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
Reputation: 580
Yes there is: parse_str
Docs; Example (Demo):
parse_str("recordsArray[]=3&recordsArray[]=1", $test);
print_r($test);
Upvotes: 7
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