How to search the associated array key value in PHP and the _php technique

Source: Internet
Author: User

1. Search Associative array keys
If a specified key is found in an array, the function array_key_exists () returns TRUE, otherwise it returns false. Its

The form is as follows:
Boolean array_key_exists (mixed key, array array)

The following example searches for the Ohio in the array key, and if found, will output the education information for the state to join the U.S. federal Government:
$state ["Delaware"]= "December 7,1787";
$state ["Pennsylvania"]= "December 12, 1787";
$state ["Ohio"]= "March l,1803";
86 5th Chapter Array
if (array_key_exists ("Ohio", $state)
printf ("Ohio joined the Union on%s", $state ["Ohio"]);
The results are as follows:

2. Searching for associative array values
The Array_search () function searches an array for a specified value and returns False if it finds the corresponding key.

The form is as follows:
The following example searches for a specific date (DECEMBER7) in $state and, if found, returns information about the corresponding state:
$state ["Ohio"] = "March L";.
$statef "Delaware" L = "December 7";
$state ["Pennsylvania"] = "December 12u;"
$founded = Array_search ("December 7", $state),
i+ ($founded) printf ("%s is founded on%s", $founded, $state [$founded]);
The output is as follows:
Delaware is 10 ounded on December 7.

5.4.2 Get array keys
The Array_keys () function returns an array that contains all the keys found in the searched array. Its form is known:
Array Array_keys (array array "J mixed Search_value])
If you include an optional parameter search value, only the key that matches that value is returned. The following example outputs the $state array
All key values found:
$state ["Delaware"] = "December 7, 1787";
$state ["Pennsylvania"] = "December, i787";
$state ["New Jersey"] = "December 18, 1787";
$keys = Array_keys ($state);
Print_r ($keys);
The output is as follows:

5.4.3 Get Array values
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)

5.5 Traversal Array 87
The following example will capture the population of the states found in the $population:
$population =array ("Ohio" => "11,421,267", "Iowa" => "2,936,760");
Print_r (Array_values ($population);
The output of this example is as follows:

5.5 Traversal Array
It's not surprising that PHP usually has to iterate over the array and get the keys or values (or both), so it's no surprise
Some functions to satisfy the requirements. Many functions can accomplish two tasks, not only to get the key or the value of the current pointer position, but also to move the pointer
Down to an appropriate position. These functions are described in this section.

5.5.1 gets the current array key
The key () function returns the keys in the input_array where the current pointer is located. The form is as follows:
Mixed key (array array)

The following example uses the iteration to process the array and move the pointer to output the key of the $capitals array:
$capitals =array ("Ohio" => "Columbus", "Iowa" => "Des Moines");
echo "<p>can you name the capitals of these states?</p>";
while ($key =key ($capitals) {
printf ("%s <br,>", $key);
Next ($capitals);

The following results are returned:
Ohio

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.