Php array operation-delete specified array element _ PHP Tutorial

Source: Internet
Author: User
Php array operation-delete a specified array element. This article condenses the usage of various php functions to remove array elements by specified element values. if you need them, refer to them. The code for removing array elements based on specified element values is as follows: This article describes the usage of various php functions to remove array elements based on specified element values. For more information, see.

Removes array elements based on specified element values

The code is as follows:

$ A = array ("a" => "Dog", "B" => "Cat", "c" => "Horse ");
Print_r ($ );
Unset ($ a [array_search ("Cat", $ a)]);
// Array_search ("Cat", $ a) returns the key name based on the element value. Keep indexes after removal
Print_r ($ );
?>

The usage of array_search is described below.

Display result

The code is as follows:

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

Before PHP 4.2.0, the function returns null instead of false in case of failure.

If the third parameter strict is set to true, the key name of the corresponding element is returned only when the data type and value are consistent.

Syntax
Array_search (value, array, strict) parameter description
Value is required. Specifies the value to be searched in the array.
Array is required. The searched array.
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:
$ A = array ("a" => "Dog", "B" => "Cat", "c" => "Horse ");
Echo array_search ("Dog", $ );
?> Output:

A

Bytes. The code for removing array elements based on specified element values is 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.