Home  >  Article  >  Backend Development  >  How to clear array elements in php

How to clear array elements in php

王林
王林Original
2021-07-05 16:33:392044browse

The way PHP clears array elements is to use the unset function, such as [unset($array[1]);]. It should be noted that if we use the unset function to clear the array elements, it will not change other keys of the array.

How to clear array elements in php

The operating environment of this article: windows10 system, php 7.3, thinkpad t480 computer.

If we want to delete an element in the array, then we can use the unset() or array_splice() method. Here we introduce the unset method.

Note: If we use the unset() method, it will not change other keys (keys). If we want to reorder other keys (keys), we can use the array_values() method.

Implementation code:

<?php
$array = array(0 => "a", 1 => "b", 2 => "c");
unset($array[1]);
//↑ 你要删除的数组元素值的键print_r($array);
?>

Output result:

Array (
    [0] => a
    [2] => c
    )

Related video tutorial sharing: php video tutorial

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

Statement:
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