How to remove value from array in PHP?

We know that in PHP an array is a built-in data type that stores a number of values in one single variable.

I have previously discussed arrays and different types of arrays in PHP in a post titled What is an associative array in PHP. In this article we will learn how to remove an item or value from an array.

There are various processes in PHP to remove value from array (by key or by value), some of which we will discuss here. At first we will implement this by using the in-built PHP functions.

Using PHP functions to remove value from array in PHP

We can remove a value from an array in PHP using unset(), array_splice(), array_diff(), array_pop() and array_shift() php functions.

Using the unset() function:

The unset() PHP function can be used to delete array items. After deletion, the index of the array items will not change.

Remove item from an indexed array by key using the unset function

In indexed array each item of an array has a numeric index. In the following example we will remove the second item of the indexed array using the unset function.

<?php  
$colors = ["blue", "green", "red"];
unset($colors[1]);
var_dump($colors);
?>

In this case, if we run the above code, we get the following output:

array(2) {
  [0]=>
  string(4) "blue"
  [2]=>
  string(3) "red"
}
Remove item from an associative array by key using the unset function

In associative array each item of an array has named keys. In the following example we will remove the array item with color key of the associative array using the unset function.

<?php  
$shape = ["shape_type"=>"circle", "color"=>"blue", "position"=>"top"];
unset($shape["color"]);
var_dump($shape);
?> 

Here after running the above code we get the following output:

array(2) {
  ["shape_type"]=>
  string(6) "circle"
  ["position"]=>
  string(3) "top"
}

In the above examples we have removed array items from the array by key. But if we need to remove the items from the above arrays by value instead of the key then we can do it using the array_search function and the unset functions together.

Remove item from an indexed array by value using the unset function

In the following example we will remove the “green” value item of the indexed array using the array_search function and the unset function. This will work properly when the value is unique.

<?php  
$colors = ["blue", "green", "red"];
$key = array_search("green", $colors);
if ($key !== false) {
    unset($colors[$key]);
}
var_dump($colors);
?>

If we run the above example in the browser we get the following output:

array(2) {
  [0]=>
  string(4) "blue"
  [2]=>
  string(3) "red"
}
Remove item from an associative array by value using the unset function

In the following example we will remove the array item with “blue” value of the associative array using the array_search function and the unset functions together. Again this will work properly when the value is unique.

<?php  
$shape = ["shape_type"=>"circle", "color"=>"blue", "position"=>"top"];
$key = array_search("blue", $shape);
if ($key !== false) {
    unset($shape[$key]);
}
var_dump($shape);
?>

If we run the above example in the browser we get the following output:

array(2) {
  ["shape_type"]=>
  string(6) "circle"
  ["position"]=>
  string(3) "top"
}

We can learn more about the unset function at: https://www.php.net/manual/en/function.unset.php.

Using the array_splice() function

The array_splice function delete a part of the arrays and replace it with something else. To remove an item from an array we specify the start index (second parameter) and how many items we want to delete (third parameter) in the array_splice function.

Remove item from an indexed array by key using the array_splice function

We will remove the second item of the indexed array using the array_splice function in the following example.

<?php  
$colors = ["blue", "green", "red"];
array_splice($colors, 1, 1);
var_dump($colors);
?>

If we run the above example in the browser, we get the following output:

array(2) {
  [0]=>
  string(4) "blue"
  [1]=>
  string(3) "red"
}

Here we see that after the removal of the array item, the array gets reindexed, starting at index 0.

Remove item from an associative array by key using the array_splice function

The array_splice function works in the same way for associative arrays as it does for indexed arrays. We will remove the second item of the associative array using the array_splice function in the following example.

<?php  
$shape = ["shape_type"=>"circle", "color"=>"blue", "position"=>"top"];
array_splice($shape, 1, 1);
var_dump($shape);
?>

After running the above example in the browser we get the following output:

array(2) {
  ["shape_type"]=>
  string(6) "circle"
  ["position"]=>
  string(3) "top"
}

We can learn more about the array_splice function at: https://www.php.net/manual/en/function.array-splice.php.

Using the array_diff() function

We can use the array_diff function to remove value from an array in PHP. The array_diff function computes the difference of arrays. This function returns a new array.

Remove item from an indexed array by value using the array_diff function

We will remove the “green” value of the indexed array using the array_diff function in the following example.

<?php  
$colors = ["blue", "green", "red"];
$colors = array_diff($colors, ["green"]);
var_dump($colors);
?>

After running the above example in the browser, we get the following output:

array(2) {
  [0]=>
  string(4) "blue"
  [2]=>
  string(3) "red"
}
Remove item from an associative array by value using the array_diff function

Now we will remove the “blue” value of the associative array using the array_diff function in the following example.

<?php  
$shape = ["shape_type"=>"circle", "color"=>"blue", "position"=>"top"];
$shape = array_diff($shape, ["blue"]);
var_dump($shape);
?>

After running the above code in the browser we get the following output:

array(2) {
  ["shape_type"]=>
  string(6) "circle"
  ["position"]=>
  string(3) "top"
}

We can learn more about the array_diff function at: https://www.php.net/manual/en/function.array-diff.php.

Using the array_pop() function:

The array_pop() function removes the last item of an array.

Remove the last item from an indexed array using the array_pop() function

An example of this is given below.

<?php  
$colors = ["blue", "green", "red"];
array_pop($colors);
var_dump($colors);
?>

The output of the above example is as follow:

array(2) {
  [0]=>
  string(4) "blue"
  [1]=>
  string(5) "green"
}
Remove the last item from an associative array using the array_pop() function

The array_pop() function works in the same way for associative arrays as it does for indexed arrays. An example of this is given below.

<?php  
$shape = ["shape_type"=>"circle", "color"=>"blue", "position"=>"top"];
array_pop($shape);
var_dump($shape);
?>

The output of the above example is as follow:

array(2) {
  ["shape_type"]=>
  string(6) "circle"
  ["color"]=>
  string(4) "blue"
}

We can learn more about the array_pop function at: https://www.php.net/manual/en/function.array-pop.php.

Using the array_shift() function:

The array_shift() function removes the first item of an array.

Remove the first item from an indexed array using the array_shift() function

An example of the array_shift() function is given below.

<?php  
$colors = ["blue", "green", "red"];
array_shift($colors);
var_dump($colors);
?>

The output of the above example is as follow:

array(2) {
  [0]=>
  string(5) "green"
  [1]=>
  string(3) "red"
}
Remove the first item from an associative array using the array_shift() function

The array_shift() function also works in the same way for associative arrays as it does for indexed arrays. An example of this is given below.

<?php  
$shape = ["shape_type"=>"circle", "color"=>"blue", "position"=>"top"];
array_shift($shape);
var_dump($shape);
?>

The output of the above example is as follow:

array(2) {
  ["color"]=>
  string(4) "blue"
  ["position"]=>
  string(3) "top"
}

We can learn more about the array_shift function at: https://www.php.net/manual/en/function.array-shift.php.

Using custom function to remove value from array in PHP

We can also remove array item from an array using custom function.

Creating custom function

Here we will create a custom function named customRemove.

function customRemove(&$array, $key){
    $new_arr = [];
    if(is_array($array) && !empty($array)){
        foreach($array as $k=>$v){
            if($k != $key){
                $new_arr[$k] = $v;
            }
        }
    }
    $array = $new_arr;
    return $array;
}

We have created an empty array named $new_arr in this function. Then used a foreach loop on the $array (array received as parameter) and assigned the new value to $new_arr if the key of the parameter and the key of the array item do not match. Finally assigned $array to $new_arr and return $array.

Remove item from an indexed array by key using custom function

In the following example we will remove the second item of the indexed array using our custom function.

function customRemove(&$array, $key){
    $new_arr = [];
    if(is_array($array) && !empty($array)){
        foreach($array as $k=>$v){
            if($k != $key){
                $new_arr[$k] = $v;
            }
        }
    }
    $array = $new_arr;
    return $array;
}

$colors = ["blue", "green", "red"];
customRemove($colors, 1);
var_dump($colors);
?>

Here we get the following output for the above code:

array(2) {
  [0]=>
  string(4) "blue"
  [2]=>
  string(3) "red"
}
Remove item from an associative array by key using custom function

In the following example we will remove the array item with color key of the associative array our custom function.

<?php  
function customRemove(&$array, $key){
    $new_arr = [];
    if(is_array($array) && !empty($array)){
        foreach($array as $k=>$v){
            if($k != $key){
                $new_arr[$k] = $v;
            }
        }
    }
    $array = $new_arr;
    return $array;
}

$shape = ["shape_type"=>"circle", "color"=>"blue", "position"=>"top"];
customRemove($shape, "color");
var_dump($shape);
?>

If we run the above code in the browser we get the following output:

array(2) {
  ["shape_type"]=>
  string(6) "circle"
  ["position"]=>
  string(3) "top"
}
Remove item from an indexed array by value using custom function

In the following example we will remove the array item with “blue” value of the indexed array using our custom function. For this we will modify the customRemove function first.

<?php  
function customRemove(&$array, $value){
    $new_arr = [];
    if(is_array($array) && !empty($array)){
        foreach($array as $k=>$v){
            if($v != $value){
                $new_arr[$k] = $v;
            }
        }
    }
    $array = $new_arr;
    return $array;
}

$colors = ["blue", "green", "red"];
customRemove($colors, "green");
var_dump($colors);
?>

If we run the above example in the browser we get the following output:

array(2) {
  [0]=>
  string(4) "blue"
  [2]=>
  string(3) "red"
}
Remove item from an associative array by value using custom function

Now we will remove the array item with “blue” value of the associative array using our custom function In the following example.

<?php  
function customRemove(&$array, $value){
    $new_arr = [];
    if(is_array($array) && !empty($array)){
        foreach($array as $k=>$v){
            if($v != $value){
                $new_arr[$k] = $v;
            }
        }
    }
    $array = $new_arr;
    return $array;
}

$shape = ["shape_type"=>"circle", "color"=>"blue", "position"=>"top"];
customRemove($shape, "blue");
var_dump($shape);
?>

If we run the above example in the browser we get the following output:

array(2) {
  ["shape_type"]=>
  string(6) "circle"
  ["position"]=>
  string(3) "top"
}