简体   繁体   中英

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.

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?

Use array_column() to create array of all present kolicina values. Then use array_filter() to filter out all values, that You consider "empty". If this results in an empty array, then all $txtHidd[...]['kolicina'] are empty.

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? 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 :-). Maybe I misunderstood Your problem, in such a case correct me, please!

Use this code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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