Php array operation-delete a specified array element

Source: Internet
Author: User
This article condenses the usage of various php functions to remove array elements based on the specified element values and remove array elements based on the specified element values. the code is as follows: & lt ;? Elements with a value of & quot; Cat & quot; removed from php $ Comment R

This article condenses the usage of various php functions to remove array elements based on the specified element values and remove array elements based on the specified element values. the code is as follows:

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

The following code describes how to use array_search:

  1. // Before removal:
  2. Array
  3. (
  4. [A] => Dog
  5. [B] => Cat
  6. [C] => Horse
  7. )
  8. // After removal:
  9. Array
  10. (
  11. [A] => Dog
  12. [C] => Horse
  13. )

Array_search () definition and usage

Like the in_array () function, array_search () finds a key value in the array. if this value is found, the key name of the matching element is returned. if this value is 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)

Parameters:Value is required, which specifies the value to be searched in the array.

Parameters:Array is required. The searched array.

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

The sample code is as follows:

  1. $ A = array ("a" => "Dog", "B" => "Cat", "c" => "Horse ");
  2. Echo array_search ("Dog", $ );
  3. ?> // Output:

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.