How to use keys (key) in PHP arrays

Source: Internet
Author: User
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) */?>
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.