In php how do I search for the associated array keys and get them

  • 2020-10-07 18:36:57
  • OfStack

1. Search for associative array keys
The function array_key_exists() returns TRUE if a specified key is found in 1 array, otherwise 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 and, if found, will output the state's entry into the FEDERAL government:
$state["Delaware"]="December 7,1787";
$state["Pennsylvania"]="December 12, 1787";
$state["Ohio"]="March l,1803";
Chapter 5 Groups of Numbers
if (array_key_exists (" Ohio ", $state"
printf("Ohio joined the Union on %s", $state["Ohio"]);
The results are as follows:

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

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 was founded on %s.", $founded, $state[$founded]);
The output is as follows:
Delaware was10ounded on December 7.

5.4.2 Get array keys
The array_keys() function returns an array containing all the keys found in the searched array. The form is as follows:
array array_keys(array array [J mixed search_value])
If the optional parameter search value is included, only the key that matches the value is returned. The following example outputs the $state array
All key values found:
$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 numeric index to the returned array. The form is as follows:
array array_values(array array)

5.5 Iterate over group 87
The following example gets 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 Iterate over the groups
You usually need to iterate over groups and get each key or value (or both), so it's not surprising that PHP provides this
1 Some functions to meet the requirements. Many functions do two things, not only getting the key or value of the current pointer position, but also moving the pointer
I'm going to go down 1. This section describes these functions.

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

The following example outputs the key of the $capitals array by iterating through the array and moving the pointer:
$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 will be returned:
Ohio


Related articles: