PHP implements methods to delete array-specific elements

Source: Internet
Author: User
This article mainly introduces the PHP delete array specified element implementation code related information, the need for friends can refer to the following

PHP Delete array Specifies the element implementation code

The function of deleting an array-specific element in PHP is not directly available, and most can only encapsulate the method

For example, the Array_slice () function takes a value out of a condition in an array and returns it.

Array_slice (Array,offset,length,preserve)

Array: Arrays
Offset: Specifies the starting position of the element to be removed. If it is a positive number, start with the previous one, and if it is negative, take the offset from the back to the absolute value.

<?php$a=array (0=> "Dog",1=> "Cat",2=> "Horse",3=> "Bird");p Rint_r (Array_slice ($a,));? >

Output

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

There is also the Array_shift () function to delete the first element in the array and return the value of the deleted element.

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

Several functions are used to feel Array_search () the utility Array_search () function, like In_array (), looks for a key value in the array. If the value is found, the key name of the matching element is returned. Returns false if not found

$array = Array (' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 '), $del _value = 3;unset ($array [Array_search ($del _value, $array)]);// Use unset to remove this element print_r ($array);

Output

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

Thank you for reading, hope to help everyone, thank you for the support of this site!

The above is the whole content of this article, I hope that everyone's study has helped.


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.