The way PHP determines whether a string exists in an array element

Source: Internet
Author: User
Tags data structures memory usage

Method One: Adopt In_array (Value,array,type)

Type is optional. If this parameter is set to True, the search data is checked for the same type as the value of the array.

. The code is as follows:

$arr = Array (' Can ', ' how ', ' method ', ' Know ', ' no ', ' no ');

In_array (Value,array,type)

$isin = In_array ("How 2", $arr);

if ($isin) {

echo "in====". $isin;

}else{

echo "out====". $isin;

}

Method Two:

The Array_key_exists ' array_key_exists () function determines whether a specified key exists in an array, returns TRUE if the key exists, or returns false. Array_key_exists (Key,array

Method Three:

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. Array_search (value,array,strict), when the amount of data is relatively large, with array_key_exists more appropriate, but the memory is larger,

The array structure is: Array (1, 2, 3, ...) and Array (1 => true, 2 => false, ...), memory usage ratio is 1:2.

Specifically related to internal implementation, in PHP, the first and second type of data structure is similar, are associative arrays.

Other supplements:

There are three ways to find whether an element is in an array:

The In_array ' function searches the array for a given value. In_array (value,array,type) type is optional. If this parameter is set to True, the search data is checked for the same type as the value of the array.

The Array_key_exists ' array_key_exists () function determines whether a specified key exists in an array, returns TRUE if the key exists, or returns false.

Array_key_exists (Key,array)

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

Array_search (value,array,strict)

From this point of view, when the amount of data is small, such as less than 1000, to find which one is OK, will not become a bottleneck;

When the amount of data is larger, it is more appropriate to use array_key_exists.

Of course, here array_key_exists occupy a larger memory, after the calculation

The array structure is: Array (1, 2, 3, ...) and Array (1 => true, 2 => false, ...)

Their memory usage ratio is 1:2;

This is related to the internal implementation, which is actually similar to the first and second data structures in PHP, and is an associative array.

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.