How to delete a key in PHP array

PHPz
Release: 2023-04-25 09:36:01
Original
624 people have browsed it

PHP is a popular server-side programming language, and arrays are also one of the most commonly used data structures in PHP. Arrays can store an ordered set of elements and can easily operate on them. However, sometimes we may need to delete an element in an array. This article will introduce how to delete a key in a PHP array.

In PHP, you can use the unset() function to delete a key from an array. This function requires one parameter, which is the name of the key to be deleted. The following is a sample code:

<?php
$my_array = array(
    "apple" => 5,
    "banana" => 10,
    "orange" => 15
);

unset($my_array["banana"]);

print_r($my_array);
?>
Copy after login

In the above code, an array $my_array containing three elements is created, and then the element with the key "banana" is deleted using the unset() function. Finally, the print_r() function is used to output the deleted array.

Run the above code, we will get the following output:

Array
(
    [apple] => 5
    [orange] => 15
)
Copy after login

We can see that the "banana" key has been successfully removed from the array.

It should be noted that using the unset() function to delete a key will not affect the key names of other elements in the array. This means that, in this example, the "apple" and "orange" keys remain unchanged.

In addition to using the unset() function, you can also use the array_splice() function to delete elements in the array. The syntax of this function is as follows:

array_splice(array, start, length)
Copy after login

Among them, array represents the array to be operated on, start represents the starting position of the element to be deleted, and length represents the number of elements to be deleted.

If you only delete one element, you can set length to 1. The following is a sample code that uses the array_splice() function to delete array elements:

<?php
$my_array = array("apple", "banana", "orange");

array_splice($my_array, 1, 1);

print_r($my_array);
?>
Copy after login

In the above code, an array $my_array containing three elements is created, and then the array_splice() function is used to delete the first element from the array. Two elements (i.e. "banana"). Finally, the print_r() function is used to output the deleted array.

Run the above code, we will get the following output:

Array
(
    [0] => apple
    [1] => orange
)
Copy after login

We can see that the "banana" element has been successfully removed from the array.

To summarize, you can use the unset() function or array_splice() function to delete elements in an array in PHP. Use these functions to remove unwanted elements from an array, making the array cleaner and easier to manipulate.

The above is the detailed content of How to delete a key in PHP array. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template