Home >Backend Development >PHP Tutorial >PHP takes the first element of the array and deletes the array instance_PHP tutorial
In PHP, array is a very special array type that can store various types of data. Below I will introduce some examples of reading and deleting specified array elements from the array.
array: array
offset: Specifies the starting position of the element to be retrieved. If it is a positive number, it is taken from the front to the back. If it is a negative value, the offset absolute value is taken from the back to the front.
1. PHP gets the first element of the index array
It is very easy to get the first element of the index array. Just write 0 as the subscript. No explanation required.
The focus is to share with you how to get the first item in an associative array in PHP. First put the code I wrote today:
The code is as follows | Copy code | ||||
//Take the default first channel name
$arr_num = count($channel_arr); |
代码如下 | 复制代码 |
输出 Array ( [0] => Cat [1] => Horse ) |
The code is as follows | Copy code | ||||
$a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird"); print_r(array_slice($a,1,2)); ?>
Array ( [0] => Cat [1] => Horse ) |
The relative array_pop() function deletes the last element in the array.
代码如下 | 复制代码 |
unset($array[3]); |
The array_search() function is the same as in_array(), searching for a key value in the array. If the value is found, the key of the matching element is returned. If not found, return false
代码如下 | 复制代码 |
unset($array[3] $array[5]); |
The code is as follows | Copy code |
$array = array('1', '2', '3', '4', '5'); $del_value = 3; unset($array[array_search($del_value, $array)]);//Use unset to delete this element print_r($array); Output array('1', '2', '4', '5'); |
The code is as follows | Copy code |
unset($array[3]); unset($array['foo']); |
The code is as follows | Copy code |
unset($array[3] $array[5]); unset($array['foo'] $array['bar']); |
To delete multiple consecutive elements, use array_splice()
The code is as follows
|
Copy code | ||||
http: //www.bkjia.com/PHPjc/631501.html