Home > Software design >  PHP - Can unset() stop the loop?
PHP - Can unset() stop the loop?

Time:11-28

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[]

<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!

CodePudding user response:

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]);
}

CodePudding user response:

Just using php filter function (https://www.php.net/manual/en/function.array-filter.php)[filter]

$count = [];
if (isset($_POST["count"])
   $count = array_filter(function($a){ return $a !== 0; }, $_POST["count"]);

print_r($count);
  •  Tags:  
  • php
  • Related