PHP Array Basic Operation method detailed, array operation method detailed _php tutorial

Source: Internet
Author: User

PHP Array Basic Operation method detailed, array operation method detailed


The concept of arrays

Array is a very important concept in PHP, we can think of arrays as a set of similar data, in fact the array is an ordered graph.

PHP also provides more than 70 built-in functions to manipulate arrays.


Create an array

Create an array using the array () language structure:

<?php$arr_age1 = Array (18, 20, 25);//or: $arr _age2 = Array ("Wang" =>18, "Li" =>20, "Zhang" =>25);//Empty array: $arr _ Age3 =array ();? >

You can also use array Controller [] to create an array:

<?php$arr_age1[] = ($arr _age1[] = ($arr _age1[] = 25;//or: $arr _age2["Wang") = $arr _age2["li"] =; $arr _age2["Z Hang "] = 25;? >

Array key names and values

An array entity contains two items: a key name and a value.

In the example below to create an array:

$arr _age1 = Array (18, 20, 25); We assign 3 array units (also called elements) to $arr _age1, with values of 18, 20, and 25, respectively. These 3 array cells are automatically assigned 3 numeric ordinals, 0, 1, and 2, respectively. That is $arr the complete structure of the _age1 array is:

Array ([0] = [1] = [2] + 25) This system automatically assigns a sequence number called the key name, and the array of keys named numeric IDs is called an indexed array (indexed array).

Of course, you can also specify the key name manually:

$arr _age1 = Array (0 = 1, 2 = 25);

Note: Manually specifying a key name may not start at 0, or you can specify a key name without specifying it in numerical order. When a new cell is added to an array without specifying a health name, the system automatically adds 1 to the largest numeric key in the existing array as the key name for the new cell.

When using a string instead of a numeric index as the key name, this array is called an associative array (associative array):

$arr _age2 = Array ("Wang" =>18, "Li" =>20, "Zhang" =>25), but in PHP there is no obvious boundary between the two arrays, and they can be used in a mixed way. Note the correlation array is sensitive to the case of key names.

Output array cell values

The output array cell values can be accessed using the following methods:

echo $arr _age1[0]; Output: 18
echo $arr _age2["Wang"]; Output: 18 In some cases in order to debug, you may need to output the entire array of data and structure, when you need to use the Print_r () or var_dump () function, see the PHP print_r and var_dump output array

Manipulating array cells

You can manipulate array cells like normal variables, such as:

<?php$arr_age2 = Array ("Wang" =>18, "Li" =>20, "Zhang" =>25), $arr _age2["Wang"] = $arr _age2["Wang"] + 10;? >

Now $arr _age2 to:

Array ([Wang] [li] = [Zhang] + 25) Check if an array unit is set, use Isset ().

Destroying an array

Use the unset () function to destroy an array cell or an entire array:

<?phpunset ($arr _age1[0]); unset ($arr _age1);? >

Multidimensional arrays

If the values in the array are also arrays, we call this array a recursive array or a multidimensional array.

Example:

<?php$all = Array ("fruits" = = Array ("A" = "orange", "b" = "banana", "c" = "apple"), "ages" = = Array (18, 2 0); Echo $all ["Fruits"] ["C"];//output Appleecho $all ["Ages"][0];//output 18?>

The above is a small series for everyone to bring the basic operation of the PHP array to explain all the content, I hope you have a lot of support to help guests home ~

http://www.bkjia.com/PHPjc/1136629.html www.bkjia.com true http://www.bkjia.com/PHPjc/1136629.html techarticle PHP Array Basic operation method detailed, array operation method coefficient array is a very important concept in PHP, we can think of arrays as a series of similar numbers ...

  • 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.