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
1 Answer 1
$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
Niet the Dark Absol
With no error checking, your code might as well be
$arr = ["status"=>30];
. Just saying...lang-php
$_GET
variable in the first place? Is it an HTML form?$_GET['status']
value containsstatus[x]
;Array ( [0] => status[30] )