How to declare arrays in php: two methods to declare arrays

Source: Internet
Author: User
There are two main ways to declare arrays in PHP: one is to apply the array () function to declare arrays, and the other is to directly declare arrays by assigning values to array elements. How does php declare an array?

There are two ways to declare arrays in PHP:: One is to apply the array () function to declare the array, and the other is to directly declare the array by assigning values to the array element. (What is a PHP array ?)

Here, the method for applying the array () function to declare an array is as follows:

array array([mixed ...])

The syntax of the parameter mixed is key => value. multiple parameters are separated by commas (,) to define the index and value respectively. Indexes can be numbers or strings. If the index is omitted, an integer index starting from 0 is automatically generated. If the index is an integer, the next index will be the largest integer index + 1 currently. If two identical indexes are defined, the subsequent index will overwrite the previous one.

The data types of each data element in the array can be different or array. when mixed is an array type, it is a two-dimensional array (about the declaration of two-digit groups, ).

When the array () function is used to declare an array, the array subscript can be either a numeric index or an associated index. Use "=>" to connect subscript and array element values. use commas to separate different array elements!

Using the array () function to define an array is flexible. you can give only the array element value in the function body, rather than the key value. For example:

 

The output result is:

Note: You can assign an array () function with no parameters to the variable to create an empty array, and then use the square brackets [] syntax to add the array element value!

PHP provides the array () function for creating arrays. When using the data of one of the elements, you can directly use the values in the order they pay attention to. this order is called the subscript of an array. For example:

 

The output result is:

Note: The array is defined using the array () function. the subscript starts from 0 by default, instead of 1, and then increases by 1. Therefore, the element whose subscript is 2 is the 3rd element of the exponent group. And so on!

The following code declares an array through the array () function:

 "PHP", "2" => "medium", "3" => "text", "4" => "net "); // declare the array print_r ($ array); // output the array element echo"
"; Echo $ array [1]; // output the value of the array element echo $ array [2]; // output the value of the array element echo $ array [3]; // output the value of the array element echo $ array [4]; // output the value of the array element?>

The output result is:

Another flexible array declaration method in PHP is to directly assign values to array elements.If you do not know the size of the created array when creating the array, or the size of the group may change when writing the program, it is better to use this method to create the array.

In order to better understand this array declaration method, the following describes the array declaration method through a specific instance. the sample code is as follows:

 

The output result is:

Note: When declaring an array by directly assigning values to an array element, the array names in the same array element must be the same.

Recommended tutorials]

1. Related topics: "php Array (Array) topics"

2. related video course recommendations: php array creation and access: 3 creation methods and 2 access methods

The above is how php declares an array: details about the two methods of declaring an array. For more information, see other related articles in the first PHP community!

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.