PHP Array Operations-delete specified array elements _php tutorial

Source: Internet
Author: User
This article condensed the use of various functions of PHP to achieve the specified element value to remove the array elements, the need for friends can refer to.

Removes an array element by the specified element value

The code is as follows Copy Code

$a =array ("a" = "Dog", "b" = "Cat", "C" and "Horse");
Print_r ($a);
unset ($a [Array_search ("Cat", $a)]);
Array_search ("Cat", $a) returns the key name by element value. Keep index after removal
Print_r ($a);
?>

The following is about Array_search usage

Show results

The code is as follows Copy Code

Before removal:
Array
(
[A] = Dog
[B] = Cat
[C] = Horse
)

After removal:

Array
(
[A] = Dog
[C] = Horse
)


Array_search () Definition and usage
The 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 it is not found.

Before PHP 4.2.0, the function returned null instead of false on failure.

If the third parameter, strict, is specified as true, the key name of the corresponding element is returned only if the data type and value are consistent.

Grammar
Array_search (value,array,strict) parameter description
Value is required. Specifies the value to search for in the array.
Array required. The array to be searched.
Strict is optional. Possible values:

True
False-Default
If the value is set to True, the type of the given value is also checked in the array. (see Example 2)

Example 1

The code is as follows Copy Code
$a =array ("a" = "Dog", "b" = "Cat", "C" and "Horse");
Echo Array_search ("Dog", $a);
?> output:

A

http://www.bkjia.com/PHPjc/631312.html www.bkjia.com true http://www.bkjia.com/PHPjc/631312.html techarticle This article condensed the use of various functions of PHP to achieve the specified element value to remove the array elements, the need for friends can refer to. Remove the array element code by the specified element value as follows ...

  • 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.