Remove Array Items in PHP
You can remove items from an array using the unset()
function for both indexed and associative arrays.
Example of Removing Array Items
<?php
$fruits = array("apple", "banana", "orange");
unset($fruits[1]); // Removes banana
print_r($fruits);
$person = array("name" => "John", "age" => 30);
unset($person['age']);
print_r($person);
?>
Output:
Array
(
[0] => apple
[2] => orange
)
Array
(
[name] => John
)
Explanation: This example demonstrates how to remove items from both indexed and associative arrays using the unset()
function.