PHP search array element method summary, php search array element _ PHP Tutorial

Source: Internet
Author: User
PHP searches for array elements. php searches for array elements. PHP searches for array elements. php searches for array elements. This article describes how to search for array elements in PHP. Share it with you for your reference. The specific analysis is as follows: PHP searches for array elements and php searches for array elements.

This example describes how to search for array elements in PHP. Share it with you for your reference. The specific analysis is as follows:

Searching, filtering, and searching array elements are common functions of array operations. The following describes several related functions.

In_array () function

The in_array () function searches for a specific value in an array. if this value is found, true is returned. otherwise, false is returned. The format is as follows:

Boolean in_array (mixed needle, array haystack [, boolean strict]);

Let's take a look at the example below to check whether the variable apple is already in the array. if so, a piece of information is output:

$ Fruit = "apple"; $ fruits = array ("apple", "banana", "orange", "pear"); if (in_array ($ fruit, $ fruits )) echo "$ fruit is already in the array ";

The third parameter is optional. it forces in_array () to consider the type during search.

Array_key_exists () function

If a specified key is found in an array, the array_key_exists () function returns true; otherwise, false. The format is as follows:

Boolean array_key_exists (mixed key, array );

The following example searches for apple in the array key. if it is found, the color of the fruit is output:

$fruit["apple"] = "red";$fruit["banana"] = "yellow";$fruit["pear"] = "green";if(array_key_exists("apple", $fruit)){ printf("apple's color is %s",$fruit["apple"]);}

The result of executing this code is as follows:
The code is as follows: apple's color is red

Array_search () function

The array_search () function searches for a specified value in an array. If yes, the corresponding key is returned. otherwise, false is returned. The format is as follows:

Mixed array_search (mixed needle, array haystack [, boolean strict])

The following example searches for a specific date (December 7) in $ fruits. if it is found, information about the corresponding state is returned:

$fruits["apple"] = "red";$fruits["banana"] = "yellow";$fruits["watermelon"]="green";$founded = array_search("green", $fruits);if($founded)  printf("%s was founded on %s.",$founded, $fruits[$founded])

The program running result is as follows:
The code is as follows: watermelon was founded on green.

Array_keys () function

The array_keys () function returns an array containing all the keys found in the searched array. The format is as follows:

Array array_keys (array [, mixed search_value])

If the optional parameter search_value is included, only the key that matches the value is returned. The following example outputs all the arrays found in the $ fruit array:

$fruits["apple"] = "red";$fruits["banana"] = "yellow";$fruits["watermelon"]="green";$keys = array_keys($fruits);print_r($keys);

The program running result is as follows:
The code is as follows: Array ([0] => apple [1] => banana [2] => watermelon)

Array_values () function

The array_values () function returns all values in an array and automatically provides a numerical index for the returned array. The format is as follows:

Array array_values (array)

The following example gets the values of each element found in $ fruits:

$fruits["apple"] = "red";$fruits["banana"] = "yellow";$fruits["watermelon"]="green";$values = array_values($fruits);print_r($values);

The program running result is as follows:
The code is as follows: Array ([0] => red [1] => yellow [2] => green)

I hope this article will help you with php programming.

Examples in this article describes how to search for array elements in PHP. Share it with you for your reference. The specific analysis 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.