Php stop explode()要求参数2为字符串警告
我有一个表单,允许用户选择2个或更多类别 php检查url,如:Php stop explode()要求参数2为字符串警告,php,get,explode,Php,Get,Explode,我有一个表单,允许用户选择2个或更多类别 php检查url,如: $categories= (!isset($_GET["categories"])?0:explode(";",$_GET["categories"])); 正常的URL是这样的 site.com/cat.php?page=&categories=1;2 is_string($_GET["category"]) 如果用户将URL更改为 site.com/cat.php?page=&
$categories= (!isset($_GET["categories"])?0:explode(";",$_GET["categories"]));
正常的URL是这样的
site.com/cat.php?page=&categories=1;2
is_string($_GET["category"])
如果用户将URL更改为
site.com/cat.php?page=&categories[1]
网站将显示
Warning: explode() expects parameter 2 to be string, array given in...
所以我试着检查$你得到这样的结果
site.com/cat.php?page=&categories=1;2
is_string($_GET["category"])
但如果运气不好,试试这个
$catString=is_string($_GET["categories"]) ? $_GET["categories"] : implode(';',$_GET["categories"]);
$categories = (!isset($_GET["categories"])?0:explode(";",$catString));
或者我想更好的解决方案是(而不是从数组转换为字符串,再转换回数组,这有点愚蠢):
如果是数组,则使用内爆或爆炸功能