Php to find the array value Program

Source: Internet
Author: User

There are many methods to find the specified value in the array in php. Next I will introduce the array search function in_array (), array_key_exists (), array_search () usage in php.

There are three methods to check whether an element is in an array:

The in_array 'function searches for the 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. Array_key_exists (key, array)

Example #1 array_key_exists () Example

The Code is as follows: Copy code

<? Php
$ Search_array = array ('first' => 1, 'second' => 4 );
If (array_key_exists ('first', $ search_array )){
Echo "The 'first' element is in the array ";
}
?>

Example #2 Comparison Between array_key_exists () and isset ()

Isset () does not return TRUE for NULL values in the array, while array_key_exists () does.

The Code is as follows: Copy code

<? Php
$ Search_array = array ('first' => null, 'second' => 4 );

// Returns false
Isset ($ search_array ['first']);

// Returns true
Array_key_exists ('first', $ search_array );
?>

Mixed array_search (mixed $ needle, array $ haystack [, bool $ strict])
The first parameter is the value to be searched, the second parameter is the array, and the last parameter is to check whether the data type is the same during the search.

The Code is as follows: Copy code

<? Php
$ A = array ("a" => "Dog", "B" => "Cat", "c" => "Horse ");
Echo array_search ("Dog", $ );
?>

<? Php
$ A = array ("a" => "5", "B" => 5, "c" => "5 ");
Echo array_search (5, $ a, true );
?>
The above code will output the following results:
B

From this point of view, when the data volume is small, such as less than 1000, finding which row is used will not become a bottleneck;
When the data volume is large, it is more appropriate to use array_key_exists.
Of course, array_key_exists occupies a large amount of memory.
Array Structure: array (1, 2, 3,...) and array (1 => true, 2 => false ,..)
Their memory usage ratio is;

Note: array_key_exist is dozens or even dozens of times more efficient than in_array.

A Simple Algorithm

Assume that the array has 1000 elements, and the key value is an unordered positive integer smaller than 1000000 and is not continuous, as shown below:

$ Arr = array (1 => 'sadas', 20 => 'asd ', 5002 => 'fghfg', 190023 => 'rty', 248 => 'kj ', 76 => 'sddd ',...);

Now, we need to obtain elements whose key values are greater than 500 and less than 600 in the array $ arr. Is there a more efficient algorithm without completely repeating the foreach statements?

Answer

The Code is as follows: Copy code


$ Result = array ();
For ($ I = 501; $ I <600; $ I ++ ){
If (! Isset ($ arr [$ I]) continue;
$ Result [] = $ arr [$ I];
}

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.