Home >Backend Development >PHP Problem >How to remove elements from array in php

How to remove elements from array in php

藏色散人
藏色散人Original
2020-07-28 10:27:102666browse

php method to delete elements from an array: first create a PHP code sample file; then define a set of array data; then delete the array element value through the "unset($array[1]);" method; finally Just output the running results.

How to remove elements from array in php

Recommended: "PHP Tutorial"

php deletes elements from an array

Delete an element in the data

If you want to delete an element in the array, you can use the unset() or array_splice() method.

If you know the value of an array element but not the key, you can use array_search() to get the key.

unset() method

Note that if you use the unset() method, it will not change other keys. If you want to change other keys (key) To rearrange the order, you can use array_values().

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

Output result:

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

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