Remove specific element from array php

This question has several answers but I want to add something more because when I used unset or array_diff I had several problems to play with the indexes of the new array when the specific element was removed (because the initial index are saved)

I get back to the example :

$array = array('apple', 'orange', 'strawberry', 'blueberry', 'kiwi');
$array_without_strawberries = array_diff($array, array('strawberry'));

or

$array = array('apple', 'orange', 'strawberry', 'blueberry', 'kiwi');
unset($array[array_search('strawberry', $array)]);

If you print the result you will obtain :

foreach ($array_without_strawberries as $data) {
   print_r($data);
}

Result :

> apple
> orange
> blueberry
> kiwi

But the indexes will be saved and so you will access to your element like :

$array_without_strawberries[0] > apple
$array_without_strawberries[1] > orange
$array_without_strawberries[3] > blueberry
$array_without_strawberries[4] > kiwi

And so the final array are not re-indexed. So you need to add after the unset or array_diff:

$array_without_strawberries = array_values($array);

After that your array will have a normal index :

$array_without_strawberries[0] > apple
$array_without_strawberries[1] > orange
$array_without_strawberries[2] > blueberry
$array_without_strawberries[3] > kiwi

Related to this post : Re-Index Array

Remove specific element from array php

Hope it will help

Description:

You need to write a program in PHP to remove specific element by value from an array using PHP program.

Instructions:

  • Take an array with list of month names.
  • Take a variable with the name of value to be deleted.
  • You can use PHP array functions or foreach loop.

With the help of array_search() function, we can remove specific elements from an array.

<?php
$delete_item = 'march';
// take a list of months in an array
$months = array('jan', 'feb', 'march', 'april', 'may');
if (($key = array_search($delete_item, $months)) !== false) {
    unset($months[$key]);
}
 
// print array to see latest values
var_dump($months);
?>

Tutorials Class - Output Window

array(4) { [0]=> string(3) “jan” [1]=> string(3) “feb” [3]=> string(5) “april” [4]=> string(3) “may” }


Solution 2: Using foreach()

By using foreach() loop, we can also remove specific elements from an array.

<?php
$delete_item = 'april';
// take a list of months in an array
$months = array('jan', 'feb', 'march', 'april', 'may'); // for april, the key is 4
foreach (array_keys($months, $delete_item) as $key) {
    unset($months[$key]);
}

// print array to see latest values
var_dump($months);
?>

Tutorials Class - Output Window

array(4) { [0]=> string(3) “jan” [1]=> string(3) “feb” [3]=> string(5) “april” [4]=> string(3) “may” }


Solution 3: Using array_diff()

With the help of array_diff() function, we also can remove specific elements from an array.

<?php
$delete_item = 'april';
// take a list of months in an array
$months= array('jan', 'feb', 'march', 'april', 'may');
$final_months= array_diff($months, array($delete_item));
 
// print array to see latest values
var_dump($final_months);
?>

Tutorials Class - Output Window

array(4) { [0]=> string(3) “jan” [1]=> string(3) “feb” [2]=> string(5) “march” [4]=> string(3) “may” }


Learn more about the similar topics:

Tutorials
PHP Arrays
Exercises & Assignments
Remove specific element by value from an array in PHP?
PHP Array to String Conversion (favourite colours chosen by user)
How to check if an array is a subset of another in PHP?
Interview Questions & Answers
No Content Found.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In order to remove an element from an array, we can use unset() function which removes the element from an array and then use array_values() function which indexes the array numerically automatically.

    Function Used:

    1. unset(): This function unsets a given variable.
      Syntax:
      void unset ( mixed $var [, mixed $... ] )
    2. array_values(): This function returns all the values from the array and indexes the array numerically.
      Syntax:
      array array_values ( array $array )

    Example 1:

    <?php 

    $arr1 = array(

        'geeks',

        'for',

        'geeks'

    );

    unset($arr1[1]); 

    var_dump($arr1);

    $arr2 = array_values($arr1);

    var_dump($arr2);

    ?>

    Output:

    array(2) {
      [0]=>
      string(5) "geeks"
      [2]=>
      string(5) "geeks"
    }
    array(2) {
      [0]=>
      string(5) "geeks"
      [2]=>
      string(5) "geeks"
    }
    

    We can also use array_splice() function which removes a portion of the array and replaces it with something else.
    Example 2:

    <?php 

    $arr1 = array(

        'geeks',

        'for',

        'geeks'

    );

    array_splice($arr1, 1, 1); 

    var_dump($arr1);

    ?>

    Output:

    array(2) {
      [0]=>
      string(5) "geeks"
      [1]=>
      string(5) "geeks"
    }
    

    PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


    How do I remove a specific element from an array in PHP?

    In order to remove an element from an array, we can use unset() function which removes the element from an array and then use array_values() function which indexes the array numerically automatically. Function Used: unset(): This function unsets a given variable.

    How do I remove a specific element from an array?

    pop() function: This method is use to remove elements from the end of an array. shift() function: This method is use to remove elements from the start of an array. splice() function: This method is use to remove elements from the specific index of an array.

    Which function is used to remove or delete elements from an array in PHP?

    The array_splice() function removes selected elements from an array and replaces it with new elements. The function also returns an array with the removed elements.

    Does PHP have flip function?

    PHP | array_flip() Function This built-in function of PHP is used to exchange elements within an array, i.e., exchange all keys with their associated values in an array and vice-versa. We must remember that the values of the array need to be valid keys, i.e. they need to be either integer or string.