PHP getting the first element of the array and deleting the array instance _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP retrieves the first element of the array and deletes the array instance. In php, arrays are a special array type that can store various types of data. next I will introduce some instances for reading and deleting specified array elements from arrays. Array: array off array in php is a special array type that can store various types of data. next I will introduce some instances for reading and deleting specified array elements from arrays.

Array: array

Offset: specifies the start position of the retrieved element. If it is a positive number, it is taken from the beginning to the end. if it is a negative value, it is taken from the back to the absolute value of the offset.

I. PHP retrieves the first index array

It is very easy to retrieve the first element of the index array. you just need to write 0 as the subscript.


I would like to share with you the first method of getting the join array from PHP. First, put the code I wrote today:

The code is as follows:
// Obtain the default First Channel name
$ Channel_arr = $ this-> get_from_channel (); // array of all channels
$ Arr_num = count ($ channel_arr );
$ First_channel = array_slice ($ channel_arr, 0,-($ arr_num-1); // obtain the first channel array
$ Html ['from _ channel'] = $ first_channel [0];


Delete an array

The code is as follows:


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

Output

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

The array_shift () function also 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.

I think array_search () is more practical when using several functions.

The array_search () function is the same as the in_array () function. you can find a key value in the array. If this value is found, the key name of the matching element is returned. If not found, false is returned.

The code is as follows:


$ 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 the array in PHP

To delete an array element in PHP, you can use the following methods:
To delete an element, use onset ()

The code is as follows:

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

To delete multiple discontinuous elements, use unset ()

The code is as follows:

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 );

Bytes. Array: array off...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.