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

How to remove elements from array in php

藏色散人
Release: 2023-03-03 12:18:02
Original
2665 people have browsed it

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);
?>
Copy after login

Output result:

Array (
    [0] => a
    [2] => c
)
Copy after login

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!

Related labels:
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