PHP Remove Array Items

To remove items from an array in PHP, you can use various methods based on your requirements, whether you want to remove a value by its key, unset specific elements, or filter elements out conditionally. 


     1. Using array_splice():


       This function removes a portion of the array and can optionally replace it with new elements.

Example
<!DOCTYPE html>
<html>
<body>
<?php
$fruits = ["apple", "banana", "cherry", "date"];
array_splice($fruits, 1, 2,"Mango"); // Removes "banana" and "cherry" and add "Mango"
print_r($fruits); // Output: ["apple", "date"]
?>
</body>
</html>

Try it yourself

     2. Using unset() to Remove an Element by Key:


      This is the simplest way to remove an element by specifying its key or index.

Example
<!DOCTYPE html>
<html>
<body>
<?php
$fruits = ["apple", "banana", "cherry"];
unset($fruits[1]); // Removes "banana"
print_r($fruits); // Output: ["apple", "cherry"]
?>
</body>
</html>

Try it yourself

Note

unset() removes the element but does not reindex numeric keys. If you want to reindex the array, use array_values().

The unset() function takes a unlimited number of arguments, and can therefore be used to delete multiple array items:

Example
<!DOCTYPE html>
<html>
<body>
<?php
$fruits = ["apple", "banana", "cherry"];
unset($fruits[1],$fruits[2]); // Removes "banana", "cherry"
print_r($fruits); // Output: ["apple", "cherry"]
?>
</body>
</html>

Try it yourself

Remove Item From an Associative Array


You can also use unset() to remove elements by their keys in an associative array.

Example
<!DOCTYPE html>
<html>
<body>
<?php
$person = ["name" => "John", "age" => 30, "gender" => "male"];
unset($person["age"]); // Removes "age"
print_r($person); // Output: ["name" => "John", "gender" => "male"]
?>
</body>
</html>

Try it yourself

Using array_diff() to Remove Specific Values:


You can remove specific values by creating a difference between arrays.

Example
<!DOCTYPE html>
<html>
<body>
<?php
$fruits = ["apple", "banana", "cherry", "banana"];
$fruits = array_diff($fruits, ["banana"]); // Removes all instances of "banana"
print_r($fruits); // Output: ["apple", "cherry"]
?>
</body>
</html>

Try it yourself

You can also use the array_diff() function to remove items from an associative array.

Example
<!DOCTYPE html>
<html>
<body>
<?php
$person = [
"name" => "John",
"age" => 30,
"gender" => "male",
"country" => "USA"
];

// Remove "male" and "USA"
$result = array_diff($person, ["male", "USA"]);
print_r($result);
?>
</body>
</html>

Try it yourself

Note

array_diff() works based on values, and it is case-sensitive.

Remove the Last Item


array_pop() removes the last element of the array.

Example
<!DOCTYPE html>
<html>
<body>
<?php
$fruits = ["apple", "banana", "cherry"];
array_pop($fruits); // Removes "cherry"
print_r($fruits); // Output: ["apple", "banana"]
?>
</body>
</html>

Try it yourself

Remove the First Item


array_shift() removes the first element of the array.

Example
<!DOCTYPE html>
<html>
<body>
<?php
$fruits = ["apple", "banana", "cherry"];

array_shift($fruits); // Removes "apple"
print_r($fruits); // Output: ["banana","cherry"]
?>
</body>
</html>

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.