Empty an Array and All References in JavaScript and PHP

Use Case: Showing competence in your programming interview

In your programming interview, you may be asked to demonstrate how to empty an array and update all references to the array's variable.

There are multiple ways to do this, but the trick is to update the references as well as the original array.

Below I'm going to show you how you can do this in both JavaScript and PHP so that you can adequately answer this question in your coding interview.

If you'd rather see this post in video format, watch the video below and be sure to subscribe to my YouTube playlist Tyler Answers Interview Questions.

JavaScript Solutions

There are a few ways to empty an array in JavaScript. If the first thing that came to your mind was a solution like this:

JavaScript

var array = [1,2,3,4,5]

var arr = array

while(array.length) {
	array.pop();
}

... then you're not alone, but there are some more simple ways to do this without creating a loop in your application.

Hey, Tyler here. I'm currently working on some great web development and digital marketing products and services over at ZeroToDigital.

If that sounds interesting to you, please check it out!

You can assign the variable the value of an empty array:

JavaScript

var array = [1,2,3,4,5]

var arr = array

array = []

console.log(arr) // [1,2,3,4,5]

But reassigning the value won't update the arr reference.

This solution, however, will empty both the original variable and all references to it:

JavaScript

var array = [1,2,3,4,5]

var arr = array

array.length = 0

console.log(arr) // []

JavaScript's native splice method on the array variable will also work when passed a single argument of 0:

JavaScript

var array = [1,2,3,4,5]

var arr = array

array.splice(0)

console.log(arr) // []

PHP Solution

Our PHP solution is a little trickier, and may require making updates to your existing application.

I'll show you why in a moment.

Just like JavaScript, this similar solution will empty the original variable but not the variables that were assigned to the value of the original variable:

PHP

$array = [1,2,3,4,5];

$arr = $array;

$array = [];

var_dump($arr); // [1,2,3,4,5]

We could use PHP's native unset function but that will only remove the existing variable, as well:

PHP

$array = [1,2,3,4,5];

$arr = $array;

unset($array);

var_dump($arr); // [1,2,3,4,5]

The problem is that by setting the value of $arr equal to (with the = sign) we are copying the variable's value, not creating a reference to it. So, when we try to empty the original array, no update is made to variables that have copied the value.

The solution to this is that when we want to create a reference to another variable in PHP, we use the & symbol, like this:

PHP

$array = [1,2,3,4,5];

$arr = &$array;

And then we can set the original array equal to an empty array and both the original and all references will be updated:

PHP

$array = [1,2,3,4,5];

$arr = &$array;

$array = [];

var_dump($arr); // []

Unset, however, will still not work as it only removes the specific, given variable.

PHP

$array = [1,2,3,4,5];

$arr = &$array;

unset($array);

var_dump($arr); // [1,2,3,4,5]

Tweet me @tylerewillis

Or send an email:

And support me on Patreon