array_search() method

The value is first looked for in the array and then deleted successively. The array_search() method is used to search the array for a specified value. If it is successful, it returns the first corresponding key. The array_search() method is case-sensitive in terms of matching the specified value with the iterated values of the array. If there are multiple occurrences of the same value, only one is fetched out of it. 

Syntax:

array_search($value, $array);

Arguments:

  • $value: Value in the array that has to be searched.
  • $array: The array in which the value is searched.

Return value: Returns the key if the value exists in the array, else returns false. 

unset() method:

The unset() function in PHP is used to reset any variable. If unset() is called upon a user-defined function, it unsets the local variables. There can be many values that can be specified as arguments of this function. At least one variable is mandatory in the unset() method. 

Syntax:

unset($var);

Return value: The unset() function does not return any value.

PHP code:

PHP
<?php
#declaring an associative array
$arr = array(
    0 => 'w3wiki',
    1 => 'Python',
    2 => 'Java',
    3 => 'Physics'
);
#printing original array
echo ("Original Array: \n");
var_dump($arr);
#declaring the value to delete
$val = "Physics";
#finding the key on the basis of value
$key = array_search($val, $arr);
if (($key) !== false)
{
    #deleting the key found
    unset($arr[$key]);
}
echo ("<br><br>Modified Array: \n");
var_dump($arr);
?>

Output
Original Array: 
array(4) {
  [0]=>
  string(13) "w3wiki"
  [1]=>
  string(6) "Python"
  [2]=>
  string(4) "Java"
  [3]=>
  string(7) "Physics"
}
Modified Array: 
array(3) {
  [0]=>
  string(13) "w3wiki"
  [1]=>
  string(6) "Python"
  [2]=>
  string(4) "Java"
}

How to perform Array Delete by Value Not Key in PHP ?

An array is essentially a storage element for key-value pairs belonging to the same data type. If keys are specified explicitly, ids beginning from 0 as assigned to the values by the compiler on their own. Arrays allow a large variety of operations, like access, modification, and deletion upon the stored elements. The element positions are then adjusted accordingly. 

Table of Content

  • array_search() method
  • array_diff() method 
  • Using array_values() with array_splice()

Similar Reads

array_search() method

The value is first looked for in the array and then deleted successively. The array_search() method is used to search the array for a specified value. If it is successful, it returns the first corresponding key. The array_search() method is case-sensitive in terms of matching the specified value with the iterated values of the array. If there are multiple occurrences of the same value, only one is fetched out of it....

array_diff() method

The array_diff() function in PHP is used to compare the values of two (or more) arrays and return the differences. It returns an array that contains the entries from array1 that are not contained in array2 or array3, etc....

Using array_values() with array_splice()

To delete array elements by value using array_values() with array_splice() in PHP, find the key of the value, splice the array at that key, then reindex the array using array_values()....

Contact Us