How to delete all elements from php array

PHPz
Release: 2023-04-26 09:52:34
Original
703 people have browsed it

In PHP, array is a very common data type. In the process of using an array, it is often necessary to delete elements in it. Generally speaking, PHP provides two methods for deleting array elements: one is to delete the element at a specified index, and the other is to delete all elements.

If you need to delete all elements in the array, you can use PHP's built-in function array_splice() or unset(). The use of these two methods will be introduced below.

Use array_splice() to delete all elements

The array_splice() function is one of the built-in functions provided by PHP and can be used to slice arrays. When the slice length is 0, all elements in the array can be deleted.

The specific operation steps are as follows:

  1. Prepare an array of elements to be deleted, for example:
$array = array(1, 2, 3, 4, 5);
Copy after login
Copy after login
  1. Use the array_splice() function to Array slicing:
array_splice($array, 0);
Copy after login

The first parameter is the array to be operated on; the second parameter is the starting position to be deleted, here is 0, which means starting from the first element of the array Delete; the third parameter is the length to be deleted, here is 0, which means that the length to be deleted is the length of the array, that is, all elements are deleted.

  1. After executing the above code, print the output array:
var_dump($array);
Copy after login
Copy after login

The output result is as follows:

array(0) {
}
Copy after login
Copy after login

As you can see, there are no elements in the array. .

Use unset() to delete all elements

In addition to using the array_splice() function, you can also use the unset() function to delete all elements in the array. The specific steps are as follows:

  1. Prepare an array of elements to be deleted, for example:
$array = array(1, 2, 3, 4, 5);
Copy after login
Copy after login
  1. Use the unset() function to delete all elements in the array:
foreach ($array as $key => $value) {
    unset($array[$key]);
}
Copy after login

Among them, use the foreach() function to traverse all elements in the array, and use the unset() function to delete each element.

  1. After executing the above code, print the output array:
var_dump($array);
Copy after login
Copy after login

The output result is as follows:

array(0) {
}
Copy after login
Copy after login

You can also see that there is no longer anything in the array element.

Summary

In PHP, to delete all elements in an array, you can use the array_splice() function or unset() function. In actual development, you can choose the corresponding method to operate according to specific needs.

The above is the detailed content of How to delete all elements from 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