How PHP declares an array: Two ways to declare an array

Source: Internet
Author: User
How does PHP declare an array?

There are two main ways to declare an array in PHP : One is to apply an array () function declaration, and the other is to declare an array directly by assigning values to the elements of the array. (What is an array of PHP?)

Where you declare an array by using the array () function is as follows:

Array array ([mixed ...])

The syntax for parameter mixed is key=>value, and multiple parameter mixed are separated by commas to define the index and value respectively. The index can be either a number or a string. If the index is omitted, an integer index starting from 0 is automatically generated. If the index is an integer, then the next resulting index will be the largest integer index +1 currently. If two identical indexes are defined, then one of the subsequent indexes will overwrite the previous one.

The data type of each data element in the array can be different, or it can be an array type, and when mixed is an array type, it is a two-dimensional array (the declaration of two-bit arrays, which we will explain in detail later).

When an array is declared using the array () function, the array subscript can be either a numeric index or an associative index. The subscript is concatenated with the array element value using "= =", and the different array elements are separated by commas!

It is more flexible to use the array () function to define arrays, but instead of giving the key values, you can give only a few sets of element values in the function body. For example:

<?php$array = Array ("ASP", "PHP", "JSP");    Defines the array print_r ($array);                      Output array element?>

The result of the output is:

Note: You can create an empty array by assigning the variable an array () function without parameters, and then use the square brackets [] syntax to add the array element values!

PHP provides an array () function for creating arrays. When you use data from one of these elements, you can use them directly in the order in which they are valued, which is called an array subscript. For example:

<?php$array = Array ("ASP", "PHP", "JSP"); Defines the array echo $array [1]                       //The second subscript value of the output array element?>

The result of the output is:

Note: The array is defined using the array () function, which defaults to 0 instead of 1, then increments by 1. So the element labeled 2 is the 3rd element of an exponential group. And so on

The following is a sample code that declares an array through the array () function:

<?phpheader ("content-type:text/html; Charset=utf-8 "); $array = Array (" 1 "=" PHP "," 2 "=" Medium "," 3 "=" Wen "," 4 "=" net ");   Declares an array of print_r ($array);                                       Output array element echo "<br>"; echo $array [1];                                      The value of the output array element echo $array [2];                                      The value of the output array element echo $array [3];                                      The value of the output array element echo $array [4];                                     The value of the output array element?>

The result of the output is:

another flexible way to declare arrays in PHP is to assign values directly to an array element. If you do not know the size of the array you are creating when you create the array, or if the size of the array may change when you actually write the program, the method used to create this array is better.

To deepen our understanding of how this array is declared, the following examples illustrate how the array is declared in this example code:

<?phpheader ("content-type:text/html; Charset=utf-8 "), $array [1]=" PHP ", $array [2]=", "$array [3]="; $array [4]= "NET";p Rint_r ($array);    Outputs the structure of the created array?>

The result of the output is:

Note: The array names in the same array element are required to be the same when declaring an array by assigning values directly to the arrays.

" Related tutorials recommended "

1. Related topics:PHP Arrays (array) topic

2. Related Video Course recommendation: "PHP array creation and access: 3 ways to create and 2 ways to access "

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.