如何在具有多个数组的 foreach 循环中检查 $_post 变量

how to check $_post variable in foreach loop with multiple arrays

我正在使用这段代码来过滤多个字段

$fields = array('ageid', 'heightid', 'waistid'); //database fields
    $txtfields = array('txtagefrom', 'txtheightfrom', 'txtwaistfrom'); //from range
    $txttofields = array('txtageto', 'txtheightto', 'txtwaistto'); // To Range


$conditions = array();
foreach (array_map(NULL, $fields, $txtfields, $txttofields) as $x) {

  list($fields, $txtfields, $txttofields) = $x;
 echo "'" . $fields . "' between '" . $txtfields . "' And '" . $txttofields . "'";
}

但我卡住了,如果某些字段为空,它也会显示在结果上,我尝试了多种方法但都失败了。

我需要,如果这些字段中的任何一个为空,那么它就不会显示在结果中。

查看完整代码here

检查 $fields 是否为空或 is_null
您的代码中还有一个拼写错误

<input type="text" name="txtwasitto" placeholder="Wasit To" />

改为

<input type="text" name="txtwaistto" placeholder="Wasit To" />

确保从 $_POST

中删除 XSS
$fields = array('ageid', null, 'heightid', null, 'waistid'); //database fields
$txtfields = array('txtagefrom', 'testnull', 'txtheightfrom', 'testnull', 'txtwaistfrom'); //from range
$txttofields = array('txtageto', 'testnull', 'txtheightto', 'testnull', 'txtwaistto'); // To Range

$conditions = array();
foreach (array_map(NULL, $fields, $txtfields, $txttofields) as $x) {
    list($fields, $txtfields, $txttofields) = $x;
    if(!empty($_POST[$txtfields]) && !empty($_POST[$txttofields]))
    echo "'" . $fields . "' between '" . $_POST[$txtfields] . "' And '" . $_POST[$txttofields] . "'";
}