An array of PHP development (1)

Source: Internet
Author: User
An array of PHP development (i)

An array is a set of data that combines a series of data to form an actionable whole. An array of arrays is an ordered set of variables in which each variable is called an element. Each element is distinguished by a special identifier, which is called a key (also known as a subscript). Each entity in the array contains two items: A key and a value. The corresponding array element can be obtained by the key value.
There are two main ways to declare an array in PHP: One is to use the array () function to declare an array, and the other is to declare an array directly by assigning a value to the element of the array.
The method of declaring a function with array () is as follows:
Array ([mixed] ...)
The syntax for parameter mixed is key =>value, and multiple parameter mixed are separated by commas, and the indexes and values are defined separately. The index value can be a string or a number. If the index is omitted, an integer index starting from 0 is automatically generated.
Use Array () to declare the array code as follows:


    
     $array = array("Jack","Bruce","hero");print_r($array);?>

The results of the operation are as follows:

one-dimensional arrays
When an element of an array is a variable, the array is called an array. A one-dimensional array is the most common array that saves only one column of content.

two-dimensional arrays
The element of an array, if it is a one-dimensional array, is said to be a two-dimensional array.
The following is a concrete example of declaring a two-dimensional array with the following code:


    
     $array =array("老师"=>array("男老师","女老师"),"动物"=>array("哺乳动物","鸟类","鱼类"));print_r($array);?>

The results of the operation are as follows:

iterating through an array
Use the foreach structure to iterate through the array. The foreach structure does not operate on the array structure itself, but rather as a backup of the array.
The code to traverse the array with a foreach structure is as follows:


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

The results of the operation are 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.