PHP Remove Array Element: unset() Guide

In PHP, the unset() function can be used to remove a specified element from an array. Here is an example:

$fruits = array("apple", "banana", "orange", "grape");

// 删除数组中的第二个元素(下标为1)
unset($fruits[1]);

// 打印结果
print_r($fruits);

The output result is:

Array
(
    [0] => apple
    [2] => orange
    [3] => grape
)

Please note that when using the unset() function to delete elements from an array, the original array’s indexes will be preserved. If you want to reindex the array, you can use the array_values() function. For example:

$fruits = array_values($fruits);
print_r($fruits);

The output result is:

Array
(
    [0] => apple
    [1] => orange
    [2] => grape
)
bannerAds