stop explode()期望参数2是字符串警告

stop explode()期望参数2是字符串警告

问题描述:

I have a form that's allow the user to select 2 or more categories.

php check url like:

 $categories= (!isset($_GET["categories"])?0:explode(";",$_GET["categories"]));

normal URL is like this

    site.com/cat.php?page=&categories=1;2

if the user change the URL to

    site.com/cat.php?page=&categories[1]

the site will show

      Warning: explode() expects parameter 2 to be string, array given in...

so I tried to check $_GET like this

     is_string($_GET["category"])

but with no luck

我有一个允许用户选择2个或更多类别的表单。 p> php check url like: p>

  $ categories =(!isset($ _ GET [“categories”])?0:explode(“;”,$ _ GET [“categories”  “])); 
  code>  pre> 
 
 

普通网址是这样的 p>

  site.com/cat.php?page  =& categories = 1; 2 
  code>  pre> 
 
 

如果用户将URL更改为 p>

  site.com/  cat.php?page =& categories [1] 
  code>  pre> 
 
 

该网站将显示 p>

 警告:爆炸 ()期望参数2是字符串,数组在... 
  code>  pre> 
 
 

中给出所以我试图像这样检查$ _GET p> is_string($ _ GET [“category”]) code> pre>

但没有运气 p> div>

Try this?

$catString=is_string($_GET["categories"]) ? $_GET["categories"] : implode(';',$_GET["categories"]);

$categories = (!isset($_GET["categories"])?0:explode(";",$catString));

or I guess a better solution would be (instead of converting from array to string and back to array which is kinda silly):

$categories = (!isset($_GET["categories"])? "" : $_GET["categories"];

$categories = is_array($categories) ? $_GET["categories"] : explode(";",$catString));

if(isset($_GET["categories"]))
{
    if(is_array($_GET["categories"]))
        $categories = implode(" ",$_GET["categories"]);
    else
        $categories = explode(";",$_GET["categories"]);
}
else
$categories='';

If it is an array use implode or explode function.