Home >Backend Development >PHP Problem >How to remove elements from array in php
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.
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!