0

我有一个 html 表单,我可以在其中选择一些选项。我想将这些值以逗号分隔写到我的数据库中。这是我的代码

$genretotal = $_POST['genre'];
$genre0 = $genretotal[0];
$genre1 = $genretotal[1];
$genre2 = $genretotal[2];
$genre3 = $genretotal[3];
$genre4 = $genretotal[4];
$genre5 = $genretotal[5];
$genre6 = $genretotal[6];
$genre7 = $genretotal[7];
$genre = $genre0 . "," . $genre1 . "," . $genre2 . "," . $genre3 . "," . $genre4 . "," . $genre5 . "," . $genre6 . "," . $genre7;

我怎样才能省略空值?

4

3 回答 3

1

为什么这样?

$genre = join(',', array_filter($_POST['genre'], function($sItem)
{
   //here I assume your 'not empty' matches PHP empty() function
   //if not, then add desired conditions
   return !empty($sItem);
}));
于 2013-08-29T08:15:48.707 回答
1

尝试implodearray_filter

implode(',', array_filter($_POST['genre']));
于 2013-08-29T08:16:53.997 回答
0
$genretotal   =  $_POST['genre'];
if(isset($genretotal) && count($genretotal)>0)
{//This check array is null or not
    $gen_arr = implode(",",$genretotal);


}//end if
 echo $gen_arr;

//这是避免空值的代码

于 2013-08-29T08:30:43.527 回答