Adding and Removing Items in PHP Arrays

When working with arrays, you may often find yourself in a position where you need to add a few extra items to the array or remove some of the items you already added. PHP has a few functions that are designed to help you with this process, allowing you to add new elements to the beginning or end of the array or to remove elements from the beginning or end of the array.

Push Pop

To begin with, I’ll explain how to add elements to or remove elements from the end of an array. There are two simple ways to add an element to the end of an array in PHP. The first is to “push” the element onto the array. This is especially helpful when you want to add multiple elements to the end of the array at once. You can do that using the array_push function with some code similar to:

$arr = array('test1','test2');
array_push($arr,'test3','liftoff');

Using the code above, we created a new array called “$arr” and added two elements to it. We then used the array_push function to add two more new elements to the array. Those two new elements would be added to the end of the array in the same order in which you listed them in the function. Therefore, the order of the elements in the array would now be ‘test1’, ‘test2’, ‘test3’, ‘liftoff’.

If you are just adding a single new element to an existing array, it’s easier to just use empty brackets. Therefore, instead of using array_push($arr,'test3'); we would use $arr[] = 'test3';.

If you, instead, want to get the last element out of an array, you can “pop” it off. Using the array_pop function, we can remove the last element in the array and we can even store that value in its own new variable. For instance, let’s say you have a file name and you want to find out what the file extension is. You could do something like the following.

$filename = 'testing.css';
$filename = explode('.',$filename);
$extension = array_pop($filename);

The code above would turn the string “testing.css” into an array, splitting it wherever a dot appears in the string. We would then end up with an array that has the elements ‘testing’ and ‘css’ in that order. We then use the array_pop function to remove the ‘css’ from the array and store it as a new variable called $extension.

Shifting and Unshifting

So, now you know how to add new elements to and remove elements from the end of an array; but you’re probably at a point in your script where you need to do the opposite. PHP offers two more functions that will allow you to add new elements to or remove elements from the beginning of the array instead of the end.

To add a new element to the beginning of an array, you would use the array_unshift function. Let’s say, for instance, you were trying to build a permalink URL based on the page name, its parent name and the parent name of that page (the original page’s grandparent?). You could easily use the array_unshift function to do something like that. I realize the example below looks kind of silly, since I have the page name, the parent name and the grandparent name all in the script; however, imagine if we just had the page name and its parent’s ID, and we had to query the database to get the name of the parent page and the ID of the grandparent page.

$pagename='testing';
$parent='category';
$grandparent='section';
$fileurl = array('testing');
array_unshift($fileurl,$parent);
array_unshift($fileurl,$grandparent);
$fileurl = implode('/',$fileurl).'.html';

Using the example above, we would end up with $fileurl looking like section/category/testing.html.

Now, let’s say you already have an array built, and you realize you need to pull the first element out of it. You would use the array_shift function to do so. Let’s say you were planning on running a foreach loop on your array, but you realized that you need to treat the first element in the array differently than the rest (maybe its a header that needs to be wrapped in h2 tags or something, whereas the rest of the array elements will all be wrapped with p tags). You might do something like:

$content = array('Title','Content paragraph',
'Another content paragraph','One more content paragraph');
echo '<h2>'.array_shift($content).'</h2>';
foreach($content as $p) {
echo '<p>'.$p.'</p>';
}

Did you like this post? Get monthly summary of our new tutorials, posts and tips to your inbox!

One Response

Post Your Comment

Your email address will not be published.