检查二维数组中数组值是否为空

时间:2015-08-18 08:34:11

标签: php arrays

我有下一个代码

$rez1 = mysqli_query($kon, "SELECT producten.*,producten.id as prodID, product_shop_tt.*, COUNT(order_details.id) AS order_count from producten
                                          INNER JOIN product_shop_tt ON producten.id = product_shop_tt.product_id
                                          LEFT JOIN order_details ON order_details.product_shop_tt_id = product_shop_tt.id
                                          WHERE product_shop_tt.shop_id = '" . $red['id'] . "'
                                          GROUP BY producten.id
                                          ORDER BY order_count DESC");
            $brRez = mysqli_num_rows($rez1);
            $i = 1;
            while($red1 = mysqli_fetch_assoc($rez1)){

                $brProizvoda = $red1["order_count"];


                if($brProizvoda >= 30){
                    $pozadina = "background-color:#C4C4B3;border-radius:10px";
                }elseif(($brProizvoda < 30) && ($brProizvoda > 10)){
                    $pozadina = "background-color:#E0E0D8;border-radius:10px";
                }else{
                    $pozadina = "background-color:#FFFFFF;border-radius:10px";
                }


                //Listamo proizvode
                echo "<div class=\"col-xs-12 col-sm-12\" style=\"". $pozadina .";margin-bottom:1px;\">
                      <input class=\"hidd\" type=\"hidden\" name=\"txtHidd[". $red1["prodID"] ."][kolicina]\" id=\"txtHidd". $i ."\" value=\"\"/>
                      <div class=\"col-sm-2 col-xs-5\" style=\"margin-left:-25px;\">
                        <div class=\"form-group\" style=\"margin:0;\">
                            <input id=\"quan". $i ."\" class=\"form-control\" type=\"number\" value=\"0\" min=\"0\" max=\"10\" onChange=\"proces('quan".$i."', 'txtHidd".$i."'); \" style=\"margin:5px 0 5px 0;\"/>
                        </div>
                      </div>
                      <div class=\"col-sm-10 col-xs-7\" style=\"padding-left:0;\">
                        ". $red1["naam"] . " (<strong>". $red1["price"] . "</strong>€) -- <i>". $red1["details"] . "</i>
                      </div>


                      </div>";
                    $i++;
            }
                //Dugmad (Nazad i naruci)
                 echo "<div style=\"clear:both;\"></div><div class=\"footer\" style=\"position: fixed;bottom: 0;width: 100%;left:0;\">
                        <a href=\"home.php\" title=\"Ga terug\" class=\"col-xs-6 col-sm-6 btn btn-info\"><span class=\"glyphicon glyphicon-chevron-left\"></span> Niets toevoegen</a>
                        <button class=\"col-xs-6 col-sm-6 btn btn-danger\" type=\"submit\" name=\"btnNaruci\" id=\"btnNaruci\">
                            Leg in winkelmand <span class=\"glyphicon glyphicon-chevron-right\"></span><span class=\"glyphicon glyphicon-chevron-right\"></span><span class=\"glyphicon glyphicon-chevron-right\"></span>
                        </button>
                    </div></form>";

我现在想检查数组 txtHidd [“。$ red1 [”prodID“]中的每个 kolicina 值。”] [kolicina ] 是空的。

我正在尝试

$empty = array_filter($_POST["txtHidd"]);

if (empty($empty)) {
    echo "Empty";
}

这样,如果所有值都为空,我会变空,但如果一个或多个值等于其他值,我也会为空。如果数组 txtHidd [“。$ red1 [”prodID“]。”] [kolicina] 中的所有 kolicina 值,则如何获取为空空?

2 个答案:

答案 0 :(得分:2)

使用array_column()创建所有当前kolicina值的数组。然后使用array_filter()过滤掉您认为&#34;空&#34;的所有值。如果这导致空数组,则所有$txtHidd[...]['kolicina']都为空。

编辑:

好的,让我们说,你有这个阵列:

$_POST['txtHidd'] = array(
    array('a' => 'b', 'c' => 'd', 'kolicina' => ''),
    array('a' => 'f', 'c' => '1', 'kolicina' => 'abc'),
    array('a' => 'x', 'c' => '6', 'kolicina' => NULL),
    array('a' => 'o', 'c' => 'u', 'kolicina' => 'xxx')
);

您想知道,如果所有kolicina值都为空,那么这样吗?所以你使用:

$kolicinas = array_column($_POST['txtHidd'], 'kolicina');

这会给你:

array('', 'abc', NULL, 'xxx')

现在您问,是否所有这些值都是空的。所以你这样做:

$non_empty_kolicinas = array_filter($kolicinas);

你得到:

array('abc', 'xxx')

这意味着,存在非空的kolicinas(无论是什么:-)。也许我误解了你的问题,在这种情况下纠正我,拜托!

答案 1 :(得分:0)

使用此代码

if(isset($_POST["txtHidd"])){
 echo "Not Empty";
}