Php searches for the existence of an element in an array.

Source: Internet
Author: User
Php searches for the existence of an element in an array.
In the php array, how does one find whether an element exists? You can consider using functions such as array_key_exists, in_array, and array_search. This article will give you a brief introduction and a reference for interested friends.

This section describes how to find an element in an array.

In php programming, there are three methods to find whether an element is in an array: the in_array function searches for a given value in the array. In_array (value, array, type) type is optional. If this parameter is set to true, check whether the data to be searched is of the same type as the value of the array.

The array_key_exists 'Array _ key_exists () function checks whether a specified key exists in an array. if the key exists, true is returned. otherwise, false is returned.

The array_search' 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.

Note: When the data volume is small, for example, less than 1000, finding which row to use will not become a bottleneck. when the data volume is large, it is more appropriate to use array_key_exists. Of course, here array_key_exists occupies a large amount of memory. it is estimated that the array structure is:

Array (1, 2, 3,...) and array (1 => true, 2 => false ,..)

The memory usage ratio is. this is related to the internal implementation. In fact, the first and second data structures in php are similar and are associated with arrays.

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.