0

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.

asked Mar 2, 2015 at 16:12
7
  • Can you show an example GET string you are working with? Commented Mar 2, 2015 at 16:13
  • 1
    Cant you simply do $arr[] = $_GET["Status"] ? Commented Mar 2, 2015 at 16:14
  • Actually, perhaps more importantly is how are you getting that $_GET variable in the first place? Is it an HTML form? Commented Mar 2, 2015 at 16:14
  • Yes, it comes from a GET form the $_GET['status'] value contains status[x]; Commented Mar 2, 2015 at 16:15
  • @JoshKirkpatrick That will give me Array ( [0] => status[30] ) Commented Mar 2, 2015 at 16:16

1 Answer 1

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

Output:

Array
(
 [status] => 30
)
answered Mar 2, 2015 at 16:17

1 Comment

With no error checking, your code might as well be $arr = ["status"=>30];. Just saying...

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.