Php deletes the implementation code of specified elements in the array, and php array element code

Source: Internet
Author: User

Php deletes the implementation code of specified elements in the array, and php array element code

Php code used to delete specified elements of an array

In PHP, functions that delete specified elements of an array are not directly usable. Most functions can only encapsulate the elements.

For example, the array_slice () function extracts a value from the Array Based on the Conditions and returns the result.

Array_slice (array, offset, length, preserve)

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.

<? Php $ 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.

Several functions are used to find a key value in the array like in_array () in the array_search () function. If this value is found, the key name of the matching element is returned. If not found, false is returned.

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

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.