Home >Backend Development >PHP Tutorial >PHP takes the first element of the array and deletes the array instance_PHP tutorial

PHP takes the first element of the array and deletes the array instance_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:57:49839browse

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
 代码如下 复制代码
//取默认第一个渠道名
$channel_arr = $this->get_from_channel();  //所有渠道数组
$arr_num = count($channel_arr);
$first_channel = array_slice($channel_arr,0,-($arr_num-1));  //取渠道数组第一个
$html['from_channel'] = $first_channel[0];
$channel_arr = $this->get_from_channel(); //All channel arrays

$arr_num = count($channel_arr);
$first_channel = array_slice($channel_arr,0,-($arr_num-1)); //Get the first one in the channel array $html['from_channel'] = $first_channel[0];


Delete array
 代码如下 复制代码

 
$a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
print_r(array_slice($a,1,2));
?>

输出

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 = array('1', '2', '3', '4', '5');

$del_value = 3;
unset($array[array_search($del_value , $array)]);//利用unset删除这个元素

print_r($array);

输出

array('1', '2', '4', '5');

Output

Array ( [0] => Cat [1] => Horse )

There is also the array_shift() function that deletes the first element in the array and returns the value of the deleted element.


The relative array_pop() function deletes the last element in the array.

 代码如下 复制代码

unset($array[3]);
unset($array['foo']);

After using several functions, array_search() is more practical

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]);
unset($array['foo'] $array['bar']);

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');
Delete an element in an array in PHP There are several ways to delete array elements in PHP: To delete an element, use onset()
The code is as follows Copy code
unset($array[3]); unset($array['foo']);
To delete multiple discontinuous elements, also use unset()
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
 代码如下 复制代码

array_splice($array $offset $length);

Copy code
array_splice($array $offset $length);

http://www.bkjia.com/PHPjc/631501.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/631501.html
TechArticle
In php, array is a very special array type that can store various types of data. Let me introduce it below Some instances of reading and deleting specified array elements from an array. array: array off...
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