How to search and obtain associated array key values ​​in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 10:26:03
Original
778 people have browsed it

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 will search for Ohio in the array key. If found, it will output the educational information of this state joining the US federal government:
$state["Delaware"]="December 7,1787";
$state["Pennsylvania"]="December 12, 1787";
$state["Ohio"]="March l,1803";
86 Chapter 5 Array
if (array_key_exists(" Ohio", $state》
printf("Ohio joined the Union on %s", $state["Ohio"]);
The results are as follows:

2. Search associative array values
The array_search() function searches for a specified value in an array and returns the corresponding key if found, otherwise it returns FALSE.

The form is as follows:
The following example searches $state for a specific date (December7), and if found, returns relevant 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 was founded on %s.", $founded, $state[$founded]);
The output is as follows:
Delaware was founded on December 7.

5.4.2 Get array keys
The array_keys() function returns an array containing all keys found in the searched array. Its form is as follows:
array array_keys(array array[J mixed search_value])
If the optional parameter search value is included, only keys matching the value will be returned. The following example will output all keys found by
in the $state array:
$state["Delaware"] = "December 7, 1787";
$state["Pennsylvania"] = "December 12 , 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 numerical index for the returned array. Its form is as follows:
array array_values(array array)

5.5 Iterating through array 87
The following example will get the state population found in $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 Traversing Arrays
Often you need to iterate over an array and get individual keys or values ​​(or get both keys and values), so not surprisingly, PHP provides
some functions for this meet needs. Many functions can accomplish two tasks, not only get the key or value at the current pointer position, but also move the pointer
to the next appropriate position. This section describes these functions.

5.5.1 Get the current array key
key() function returns the key at the current pointer position in input_array. Its form is as follows:
mixed key(array array)

The following example prints the keys of the $capitals array by iterating through the array and moving the pointers:
$capitals=array("Ohio"=>"Columbus", "Iowa"=>"Des Moines") ;
echo "

Can you name the capitals of these states?

";
while($key=key($capitals》{
printf("%s < br,>",$key);
next($capitals);
,
will return the following results:
Ohio

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824840.htmlTechArticle1. Search for associative array keys. If a specified key is found in an array, the function array_key_exists() returns TRUE, Otherwise return FALSE. Its form is as follows: boolean array_key_exists(mix...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!