PHP array_push, array_pop, array_shift, array_unshift

Every once and a while I like to take a closer look at a particular function that I use quite frequently. This let's me make sure I'm using that particular function to the best possible effect. I find it also shifts the center of attention from a task to how the function operates. A different look so to speak. Today, I want to cover the PHP array manipulator family functions that include: array_push, array_pop, array_shift and array_unshift.

All the examples below are visualized with my free, open-source, variable checker that you are welcome to download from my newchk site at Documentation on how to use it is on the site.


Array_push($new_array, $var1, $var2, ...) pushes one or more array elements on to the end of an array, in this case, we'll use $new_array as our array. Seems simple enough. I want to create a new array, and add some variables to it. Let's take a look.

Let's set up our new array.

$new_array = array();
$var1 = "buick";
$var2 = "ford";
$var3 = "toyota";
array_push($new_array, $var1, $var2, $var3);

Let's take a look at $new_array;

If $new_array already exists with elements in it, then new variables are appended to the end of the already existing array. Let's show that.

$var_food1 = "cheese";
$var_food2 = "meat";
$var_food3 = "potatoes";
array_push($new_array, $var_food1, $var_food2, $var_food3);

Here's what the $new_array looks like now.

Some things to note about the array_push() function. It is a function, so there is no equal sign used with array_push. The order the variables are pushed on to the end of the array are in the order their listed in array_push. We can also use associative keys with array_push(), like so.


$var_ball1 = "football";
$var_ball2 = "the rock";
$var_ball3 = "hardball";
$ball_array = array("ball_football" => $var_ball1, "ball_basketball" =>$var_ball2, "ball_baseball" =>$var_ball3);
array_push($new_array, $ball_array );

Here's what the $new_array looks like now.

We've done a little more here than just assign associative keys to the array. We've jumped to what is called a multi-dimensional array. We now have an array within an array. If you look at the code above you see when working with multi-dimensional arrays, you build the inner array first, in this case the $ball_array, and then do an array_push using $ball_array as the variable in the array_push.

What got me to write this article was I was trying to do an array_push in a foreach loop using a multi-dimensional array. This is not well documented on the Internet. I had to look for awhile to find the proper syntax. When that happens I tend to write an article about it. The trick is to make a separate array variable before doing the array_push, and then add that array as a variable to the array_push().

Where do you most often use the array_push() function? Most of the time it's with a foreach loop, where your repetitively looping through an array, and creating a new subset of the array with array_push. Let's see.

$for_array = array();
$i = 1;
foreach( $new_array as $row)
	if($i < 7 )
		array_push($for_array, $row );
	$i ++;


Here, with a foreach loop, we've removed the second array, and are back to a one dimensional array, called $for_array.


Array_pop($array) pops an element off the end of an array. Let's do it


$spuds = "";
$spuds = array_pop($for_array);

Our $for_array now is minus the last element in the previous array. That element is now in a separate $variable called $spuds. Array_pop() removes the last element in the array, which you can then put into a separate variable.


What if we wanted to remove the first element in the array, instead of the last, like we do with array_pop(). We'll we use array_shift to do this, like so.

$gm = "";
$gm = array_shift($for_array);

The $for_array we started with is getting shorter as the first variable in the array is removed, and put, in this case, in the variable $gm.


Finally, since array_push added new variables to the end of the array, you probably have guessed that there is a function that adds variable to the front of the array, and you'd be right. I present to you, array_unshift(). Array_unshift() puts the new variable as the first element in your array. Let's do it.

$gm2 = "cadillac";
array_unshift($for_array, $gm2);

Our array now looks like this.

We've replaced the "buick" we've taken off the top of the array with array_shift, and upgraded to a "cadillac" with array_unshift. Nice trade-up.

If your working with multi-dimensional arrays, the key is to treat the nested arrays as a single array variable, and then use the four functions we've covered: array_push, array_pop, array_shift and array_unshift with that single array variable.


PHP array_push, array_pop, array_shift, array_unshift — 2 Comments

    • Thanks, Tezzz, for pointing this out. You can actually see the renumbering of the array keys in both the array_shift and the array_unshift functions in the article. This could get you in real trouble, if your using the array index as a database id.