PHP find and search array elements method Summary, PHP find array elements _php tutorial

Source: Internet
Author: User
Tags pear

PHP lookup and search array element method Summary, PHP lookup array elements


The example in this article describes the PHP lookup and search array element method. Share to everyone for your reference. The specific analysis is as follows:

Finding, filtering, and searching array elements are some of the common features of array manipulation. Here are a few related functions.

In_array () function

The In_array () function searches for a specific value in an array rollup and returns True if this value is found, otherwise false. The form is as follows:

Boolean In_array (mixed Needle,array Haystack[,boolean strict]);

Look at the following example, look for the variable if Apple is already in the array, and if so, output a piece of information:

$fruit = "Apple"; $fruits = Array ("Apple", "banana", "orange", "pear"), if (In_array ($fruit, $fruits))  echo "$fruit Already in the array ";

The third parameter is optional, which forces In_array () to consider the type when searching.

Array_key_exists () function

If a specified key is found in an array, the function array_key_exists () returns True, otherwise false is returned. The form is as follows:

Boolean array_key_exists (mixed Key,array array);

The following example searches for Apple in the array key and, if found, outputs the color of the fruit:

$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:
Copy the code as follows: Apple's color is red

Array_search () function

The Array_search () function searches for a specified value in an array, returns the corresponding key if found, otherwise returns false. The form is as follows:

Mixed Array_search (mixed Needle,array Haystack[,boolean Strict])

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

$fruits ["apple"] = "red", $fruits ["banana"] = "yellow", $fruits ["Watermelon"]= "green"; $founded = Array_search ("Green", $fruits); if ($founded)  printf ("%s is founded on%s.", $founded, $fruits [$founded])

The results of the program run as follows:
Copy the Code code as follows: Watermelon was founded on green.

Array_keys () function

The Array_keys () function returns an array that contains all the keys found in the searched array. The form is as follows:

Array Array_keys (array 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 results of the program run as follows:
Copy the Code code as follows: Array ([0] = Apple [1] = banana [2] = watermelon)

Array_values () function

The Array_values () function returns all the values in an array and automatically provides a numeric index for the returned array. The form is as follows:

Array array_values (array 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 results of the program run as follows:
The copy Code code is as follows: Array ([0] = red [1] = yellow [2] = = green)

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1015425.html www.bkjia.com true http://www.bkjia.com/PHPjc/1015425.html techarticle PHP lookup and search array elements method Summary, PHP find array elements in this article, the example describes the PHP lookup and search array element method. Share to everyone for your reference. The specific analysis is as follows: ...

  • Related Article

    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.