Peon
Peon

Reputation: 8020

Turn string into array

This should be a simple task, but I guess my head is a bit overheated currently.

How do I correctly turn a GET string with the value "status[30]" into an array, like:

array ( status => 30 );

I could use something like this:

$arr = array ( 'status' => str_replace( array( 'status[', ']' ), null, $_GET['status'] ) );

but there has to be a better way.

Upvotes: 0

Views: 74

Answers (1)

mhall
mhall

Reputation: 3701

$arr = [];
$getValue = "status[30]";

if (preg_match('#(\w+)\[(\w+)\]#', $getValue, $matches))
    $arr[$matches[1]] = $matches[2];

print_r($arr);

Output:

Array
(
    [status] => 30
)

Upvotes: 2

Related Questions