Php development array (1)

Source: Internet
Author: User
Php development array (1) an array is a collection of data. it combines a series of data to form an operable whole. An array is a group of ordered variables, each of which is called an element. Each element is distinguished by a special identifier, which is called a key (also known as a subscript ). Each object in the array contains two items: Key and value. You can use the key value to obtain the corresponding array elements.
In php, there are two main methods to declare arrays: one is to use the array () function to declare arrays, and the other is to directly assign values to array elements to declare arrays.
The method to declare a function using array () is as follows:
Array ([mixed]…)
The syntax of the parameter mixed is key => value. multiple parameters are separated by commas (,), which define the index and value respectively. The index value can be a string or number. If the index is omitted, an integer index starting from 0 is automatically generated.
The following code uses array () to declare an array:

 

The running result is as follows:

One-dimensional array
When an array element is a variable, it is called an array. A one-dimensional array is the most common array, which stores only one column of content.

Two-dimensional array
If an array element is a one-dimensional array, it is called a two-dimensional array.
The following code uses a specific example to declare a two-dimensional array:

 Array ("male", "female"), "animal" => array ("Mammals", "birds", "fish ")); print_r ($ array);?>

The running result is as follows:

Traverse arrays
Use the foreach structure to traverse the array. The foreach structure is not an operation on the array structure, but a backup operation on the array.
The code for traversing an array using the foreach structure is as follows:

 'jack is a teacher', 'Lisa'=>'Lisa is an actress', 'Fits'=>'Fits is a scientist' ); foreach($array as $value){ echo $value."\n"; } ?>

The running result is as follows:

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.