PHP remove element from array by value
In this article, you will learn different ways to remove an element from an array by value. An array is a collection of key-value pairs. We generally used an array to store more than one value in a single variable. PHP provides several inbuilt functions to perform operations on an array. It makes it simple and easy to remove elements from an array. Let's know about some of the predefined array methods to remove an element from an array by value.
PHP unset() function
PHP provides the unset() function to destroy a specific variable. In the given example, we have removed a specified element from an array.
<?php
$arr = array('India', 'China', 'America', 'Italy');
unset( $arr[2] ); // unset 3rd element
print_r($arr);
?>
The above code returns the following output-
Array ( [0] => India [1] => China [3] => Italy )
PHP array_diff() remove element by value
In the above example, we have removed elements by key. Let's know how the array_diff() method is used to remove the array element by value.
<?php
#Function to remove element
function removeElement($array,$value) {
return array_diff($array, (is_array($value) ? $value : array($value)));
}
# Array
$arr = array(101 =>'India', 102 => 'China', 103 => 'America', 104 => 'Italy');
$return = removeElement($arr,'China');
print_r($return);
?>
The above code returns the following output-
Array ( [101] => India [103] => America [104] => Italy )
PHP array_diff() remove element by value
In the above example, we have removed elements by key. Let's know how the array_diff() method is used to remove the array element by value.
PHP array_search() remove element by value
We can also use the array_search() method to remove the array element by value. But, it has some limitations. It can only remove the first specified element. If there are two matching elements found, then it will remove the first element only.
<?php
#Function to remove element
function removeElement(&$array,$value) {
if(($key = array_search($value,$array)) !== false) {
unset($array[$key]);
}
}
# Array
$arr = array(101 =>'India', 102 => 'China', 103 => 'USA', 104 => 'Italy');
removeElement($arr,'USA');
print_r($arr);
?>
The above code returns the following output-
Array ( [101] => India [102] => China [104] => Italy )
Related Articles
PHP reverse a string without predefined functionPHP random quote generator
PHP convert string into an array
PHP remove HTML and PHP tags from string
Import Excel File into MySQL using PHP
PHP array length
Import Excel File into MySQL Database using PHP
PHP String Contains
PHP remove last character from string
PHP calculate percentage of total
PHP qr code generator
PHP import Excel data to MySQL using PHPExcel
How to display PDF file in PHP from database
How to read CSV file in PHP and store in MySQL
Create And Download Word Document in PHP
PHP SplFileObject Standard Library
Simple File Upload Script in PHP
Sending form data to an email using PHP
Recover forgot password using PHP and MySQL
Php file based authentication
Simple PHP File Cache
How to get current directory, filename and code line number in PHP