1. Determine if the specified key exists in the array
PHP has two functions to determine if the array contains the specified keys, array_key_exists and Isset, respectively.
Array_key_exists syntax is as follows
Array_key_exists ($key, $array)
If the key exists returns true, the Isset function syntax is as follows
Isset ($array [$key])
Returns true if the key exists
The demo code is as follows:
<?php$array = Array ("Zero" and "PHP", "one" = "Perl", "one" = "Java");p rint ("is ' one ' defined?"). Array_key_exists ("One", $array). " \ n ");p rint (" is ' 1 ' defined? "). Array_key_exists ("1", $array). " \ n ");p rint (" is ', ' defined? "). Isset ($array ["" "])." \ n ");p rint (" is ' 2 ' defined? "). Isset ($array [2]). " \ n ");? >
The returned results are as follows:
Is ' one ' defined? 1Is ' 1′defined? Is ' both ' defined? 1Is ' 2′defined?
2. Some tips for using array key names
$arr [true] is equivalent to $arr [1]; $arr [false] is equivalent to $arr [0].
Make null a key name, equivalent to creating or overwriting a $arr[null], which can be accessed using $arr[null] or $arr[""].
When using a number with a decimal point as the key name, the key name automatically intercepts the integer part as the key name. As $arr[123.45]=5, you can get the key value using $arr[123.45] or $arr[123], and use a foreach traversal with $arr[123].
$arr []=5, the element is added after the array $arr.
Note: The data type of the key name in the array is an integer or string type
3. Array gets the key name based on the value
The PHP array gets the key name function based on the value, with two built-in functions available. Array_search and Array_keys, which are used to handle returning a single key name and multiple key names.
Specific examples are as follows:
<?php/** * PHP array get key by value * PHP arrays get key names by value */$items = array ( "banana" = "fruit", "tomato" and "=" Vegetable ", " lentil "and" beans ", " apple " =" vegetable "); Returns a key name if the value has a repeat return first key name $key = Array_ Search (' vegetable ', $items); echo $key;//tomato//2. Returns multiple key names $keys=array_keys ($items, ' vegetable '); Print_r ($keys);/*array ( [0] = tomato [1] = apple) */?>