Join WhatsApp ChannelJoin Now

How To Set All Zero In Last Position Using PHP 8

Hi Dev,

Today, i we will show you how to set all zero in last position using PHP 8. This article will give you simple example of how to set all zero in last position using PHP 8. you will learn get how to set all zero in last position using PHP 8. So let’s follow few step to create example of how to set all zero in last position using PHP 8.

Example

<?php
    $array = [0,0,3,4,5,6,6];
    
    function move_zero($array)
    {
        $count = 0;
        $arr_size= sizeof($array);

        for ($i = 0; $i < $arr_size; $i++ ){
            if ($array[$i] != 0) {
                $array[$count++] = $array[$i];
            }
        }

        while($count < $arr_size){
            $array[$count++] = 0;
        }

        return $array;
    }

    print_r(move_zero($array));
?>

Output:

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

I hope it will assist you…

Recommended Posts