PHP Program to Move All Zeroes to End of Array
Last Updated :
22 Jul, 2024
Given an array of random numbers, Push all the zero's of a given array to the end of the array. For example, if the given arrays is {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, it should be changed to {1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0}. The order of all other elements should be same. Expected time complexity is O(n) and extra space is O(1).
Example:
Input : Arr = [ 1, 2, 0, 4, 3, 0, 5, 0 ]
Output : [ 1, 2, 4, 3, 5, 0, 0 ]
Input : Arr = [ 1, 2, 0, 0, 0, 3, 6 ]
Output : [ 1, 2, 3, 6, 0, 0, 0 ]
There can be many ways to solve this problem. Following is a simple and interesting way to solve this problem.
Traverse the given array 'arr' from left to right. While traversing, maintain count of non-zero elements in array. Let the count be 'count'. For every non-zero element arr[i], put the element at 'arr[count]' and increment 'count'. After complete traversal, all non-zero elements have already been shifted to front end and 'count' is set as index of first 0. Now all we need to do is that run a loop which makes all elements zero from 'count' till end of the array.
Below is the implementation of the above approach.
PHP
<?php
// A PHP program to move all
// zeroes at the end of array
// Function which pushes all
// zeros to end of an array.
function pushZerosToEnd(&$arr, $n) {
// Count of non-zero elements
$count = 0;
// Traverse the array. If
// element encountered is
// non-zero, then replace
// the element at index
// 'count' with this element
for ($i = 0; $i < $n; $i++)
if ($arr[$i] != 0)
// Here count is incremented
$arr[$count++] = $arr[$i];
// Now all non-zero elements
// have been shifted to front
// and 'count' is set as index
// of first 0. Make all elements
// 0 from count to end.
while ($count < $n)
$arr[$count++] = 0;
}
// Driver Code
$arr = array(1, 9, 8, 4, 0, 0,
2, 7, 0, 6, 0, 9);
$n = sizeof($arr);
pushZerosToEnd($arr, $n);
echo "Array after pushing all " .
"zeros to end of array :";
for ($i = 0; $i < $n; $i++)
echo $arr[$i] . " ";
?>
OutputArray after pushing all zeros to end of array :1 9 8 4 2 7 6 9 0 0 0 0
Time Complexity: O(n) where n is number of elements in input array.
Auxiliary Space: O(1)
Please refer complete article on Move all zeroes to end of array for more details!
Similar Reads
Javascript Program to Move all zeroes to end of array Given an array of random numbers, Push all the zero's of a given array to the end of the array. For example, if the given arrays is {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, it should be changed to {1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0}. The order of all other elements should be same. Expected time complexity i
3 min read
Move all zeros to end of array Given an array of integers arr[], the task is to move all the zeros to the end of the array while maintaining the relative order of all non-zero elements.Examples: Input: arr[] = [1, 2, 0, 4, 3, 0, 5, 0]Output: arr[] = [1, 2, 4, 3, 5, 0, 0, 0]Explanation: There are three 0s that are moved to the end
15 min read
Write a program to display Reverse of any number in PHP ? Write a program to reverse the digits of an integer. Examples : Input : num = 12345 Output: 54321 Input : num = 876 Output: 678 It can be achieved using Iterative way or Recursive Way Iterative Method: Algorithm: Input: num (1) Initialize rev_num = 0 (2) Loop while num > 0 (a) Multiply rev_num by
2 min read
How to properly Format a Number With Leading Zeros in PHP ? A number is essentially a sequence of digits stacked together to form an integer or string. A number can begin with leading zeros. A number can also be looped through and modifications can be made to append or concatenate another sequence of characters into it. Approach 1: Using a for loop for strin
4 min read
Creating a Zero-Filled Array in PHP Creating an array filled with zeros can be useful in various scenarios, such as initializing an array for further manipulation or setting up a default state. This article explores multiple approaches to creating a zero-filled array in PHP. A zero-filled array is an array where each element is initia
3 min read
Remove First Element from an Array in PHP Given an array, the task is to remove the first element from an array in PHP. Examples:Input: arr = [1, 2, 3, 4, 5, 6, 7]; Output: 2, 3, 4, 5, 6, 7 Input: arr = [3, 4, 5, 6, 7, 1, 2] Output: 4, 5, 6, 7, 1, 2Below are the methods to remove the first element from an array in PHP:Table of ContentUsing
3 min read
How to reset Array in PHP ? You can reset array values or clear the values very easily in PHP. There are two methods to reset the array which are discussed further in this article. Methods: unset() Functionarray_diff() Function Method 1: unset() function: The unset() function is used to unset a specified variable or entire arr
2 min read
How to get elements in reverse order of an array in PHP ? An array is a collection of elements stored together. Every element in an array belong to a similar data type. The elements in the array are recognized by their index values. The elements can be subjected to a variety of operations, including reversal. There are various ways to reverse the elements
4 min read
How to Switch the First Element of an Arrays Sub Array in PHP? Given a 2D array where each element is an array itself, your task is to switch the first element of each sub-array with the first element of the last sub-array using PHP.Example:Input: num = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']];Output: [ ['g', 'b', 'c'], ['d', 'e', 'f'], ['a', 'h', '
2 min read
How to Slice an Array in PHP? In PHP, slicing an array means taking a subset of the array and extracting it according to designated indices. When you need to extract a subset of elements from an array without changing the original array, this operation comes in handy. PHP comes with a built-in function called array_slice() to he
2 min read