I have a strange error with a $_GET Value. Im using thsi code for a query: array($_GET['cats'])
If I insert the get parameter manually, like: array(3,328)
everything works fine. But if I use: array($_GET['cats'])
and submit the cats by URl like ?cats=3,328
it does not work, What could be the issue?
You can't plug a value like that. array($_GET['cats'])
is equivalent to array('3,328')
, if the value of $_GET['cats']
is 3,328
. So basically, the value is a string, not a list of integers. What you want is:
explode(',', $_GET['cats'])
$_GET['cats']
is a simple string. If you want to get 3 and 328 as seperate values you need to use explode
. You can than use foreach
to print your exploded values.
You need to split up the GET-Parameters
$values = explode(',', $_GET['cats'])
array($_GET['cats'])
will create an array containing the single element that’s value is the value of $_GET['cats']
, no matter what value it is. In case of the string value 3,328
is would be identical to array('3,328')
.
If you want to turn the string value 3,328
into an array identical to array(3,328)
, use explode
to split the string at ,
into strings and array_map
with intval
to turn each string into an integer:
$arr = array_map('intval', explode(',', $_GET['cats']));
Now this resulting array is really identical to array(3,328)
:
var_dump($arr === array(3,328)); // bool(true)
As others have said, $_GET['cats'] is a string as you're doing things at the moment.
However, if you change your URI querystring to ?cats[]=3,328
then $_GET['cats']
will be the array(3,328)
ready for you to use.