简体   繁体   English

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

[英]Checking if array value is empty in twodimensional array

I have next code 我有下一个代码

$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>";

I want now to check if every kolicina value in array txtHidd[". $red1["prodID"] ."][kolicina] is empty. 我现在要检查数组txtHidd [“。$ red1 [” prodID“]。”] [kolicina]中的每个kolicina值是否为空。

I'm trying with 我正在尝试

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

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

This way I get empty if all values are empty, but i also get empty if one or more values are equal to something else. 这样,如果所有值都为空,我将为空,但是如果一个或多个值等于其他值,我也将为 How to get empty only if all kolicina values in array txtHidd[". $red1["prodID"] ."][kolicina] are empty? 仅当数组txtHidd [“。$ red1 [” prodID“]。”] [kolicina]中的所有kolicina值都为空时,才如何获取空?

Use array_column() to create array of all present kolicina values. 使用array_column()创建所有当前kolicina值的数组。 Then use array_filter() to filter out all values, that You consider "empty". 然后使用array_filter()过滤掉您认为“空”的所有值。 If this results in an empty array, then all $txtHidd[...]['kolicina'] are empty. 如果这导致一个空数组,则所有$txtHidd[...]['kolicina']都是空的。

EDIT: 编辑:

Ok, let's say, You have this array: 好的,假设,您有以下数组:

$_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')
);

You want to know, if all the kolicina values are empty, is that so? 您想知道,如果所有kolicina值都为空,是吗? So You use: 因此,您使用:

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

This gives you: 这给您:

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

Now You are asking, if all those values are empty. 现在,您要问是否所有这些值都为空。 So You do this: 所以你这样做:

$non_empty_kolicinas = array_filter($kolicinas);

You get: 你得到:

array('abc', 'xxx')

That means, that there exist non-empty kolicinas (whatever it is :-). 这意味着,存在非空的kolicinas(无论是什么:-)。 Maybe I misunderstood Your problem, in such a case correct me, please! 也许我误解了您的问题,在这种情况下,请纠正我!

Use this code 使用此代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM