How To Set All Zero In Last Position Using PHP Example

1posts.jpg

Hi guys,

Today i will explained How To Set All Zero In Last Position Using PHP. This example is so easy to use in php.

This example to i am create a function and set the value in array. The next to set the position in Zero in last. I am use This array sequence data(0,0,3,4,5,6,6) you can use your own array data.

So let's start to the example.

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 )

Now you can check your own.

I hope it can help you...