Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP – Can unset() stop the loop?

I’m trying to remove all the element of PHP array if the value equals zero

I’m trying lots of way and reading the unset document but I’m still stuck at there

My HTML page. I have 9 input type = number with name = count[]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<form action="send.php" method="post">
    <div>
        <input type="number" name="count[]" id="count-1" value="0" min="0" max="99">
    </div>
    <div>
        <input type="number" name="count[]" id="count-2" value="0" min="0" max="99">
    </div>
    <div>
        <input type="number" name="count[]" id="count-3" value="0" min="0" max="99">
    </div>
    ...
    <div>
        <input type="number" name="count[]" id="count-9" value="0" min="0" max="99">
    </div>
    <div>
        <input type="submit" value="submit" name="submit">
    </div>
</form>

And this is my send.php file

I mean if an element of $count equals zero, I will remove that element from array. But it’s seem like somethings was wrong.

<?php
if (isset($_POST)) {
    $count = $_POST["count"];
    for ($i = 0; $i < count($count); $i++) {
        if (0 == (int) $count[$i])
            unset($count[$i]);
    }
}

This is my POST data

Array
(
    [count] => Array
        (
            [0] => 1
            [1] => 1
            [2] => 1
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
        )

    [submit] => submit
)

And this is the data of $count array I have been resulted. I don’t know why the if statement not working with some of last elements.

Array
(
    [0] => 1
    [1] => 1
    [2] => 1
    [6] => 0
    [7] => 0
    [8] => 0
)

I’m using PHP 8

Thank you!

>Solution :

Instead of iterating through the array and checking each value, you can use array_filter to remove zeros. By default array_filter removes empty values, one of which is 0 and '0' in PHP.
Here’s a working demo of using array_filter.

$count = array_filter($_POST["count"]);

Your current implementation won’t work because you are using count inside the for statement. Since you’re unset-ing the array values the count will be less for the next iterator of the loop.

A simple example is like this; the first iteration count is 7. After two iterations we unset a value from the array. In the next iteration count will return 6. With each unset you’re decreasing the value returned by count while you’re increasing $i. A fix for this would be to store the initial count before the loop so it’s a fixed value during the loop. Here’s a working demo or that.

$numOfCounts = count($count);

for ($i = 0; $i < $numOfCounts; $i++) {
    if (0 == (int) $count[$i])
        unset($count[$i]);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading