Home>Article>Backend Development> What are the ways to create arrays in php
How to create an array in php: 1. Declare the array by direct assignment, the syntax is "$array variable name [subscript] = value"; 2. Use the array() function to declare the array, the syntax is "$array variable name=array(key1=>value1,key2=>value2,...,keyN=>valueN);".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Method 1: Direct assignment method Declaring an array
When each element in the array is a specific value rather than an array, we call such an array a one-dimensional array. One-dimensional arrays are the simplest and most commonly used arrays.
The syntax format for declaring a one-dimensional array using the direct assignment method to array elements is as follows:
$数组变量名[下标] = 值
The subscript (index value) can be a string or an integer, and the subscript Requires the [ ] package.
The sample code is as follows:
'; var_dump($array); ?>
The running results are as follows:
array (size=4) 0 => string '苹果' (length=6) 1 => string '香蕉' (length=6) 2 => string '橘子' (length=6) 3 => string '榴莲' (length=6)
Tip: In addition to using the var_dump() function to print the entire array, you can also use the print_r() function.
There is no size limit for arrays in PHP, so in the above array, you can continue to add new elements to the array in the same way. When accessing elements in an array, you can use "$array variable name [subscript]
". The sample code is as follows:
'; echo '$array[1] = ' . $array[1] . '
'; echo '$array[2] = ' . $array[2] . '
'; echo '$array[3] = ' . $array[3] . '
'; ?>
The running result is as follows:
$array[0] = 苹果 $array[1] = 香蕉 $array[2] = 橘子 $array[3] = 榴莲
Statement When indexing an array, if the index value is increasing, we do not need to specify a specific index value within the square brackets. In this case, the index value starts from 0 and increases sequentially by default. The sample code is as follows:
'; var_dump($array); ?>
The running result is as follows:
array (size=4) 0 => string '苹果' (length=6) 1 => string '香蕉' (length=6) 2 => string '橘子' (length=6) 3 => string '榴莲' (length=6)
Method 2: Use the array() function to declare an array
Another way to declare an array The method is to use the array() function to create a new array. It accepts a certain number of key=>value parameter pairs separated by commas. The syntax format is as follows:
$数组变量名 = array(key1 => value1, key2 => value2, ..., keyN => valueN);
The sample code is as follows:
'红色', 1 => '黄色', 2 => '蓝色', 3 => '紫色'); echo ''; var_dump($array); ?>The running result is as follows:
array (size=4) 0 => string '红色' (length=6) 1 => string '黄色' (length=6) 2 => string '蓝色' (length=6) 3 => string '紫色' (length=6)If the => symbol is not used to specify the subscript, the default is the index array. The default index value also starts from 0 and increases in sequence. The sample code is as follows:
'; var_dump($array); ?>The running results are the same as those of the previous example.
array (size=4) 0 => string '红色' (length=6) 1 => string '黄色' (length=6) 2 => string '蓝色' (length=6) 3 => string '紫色' (length=6)Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What are the ways to create arrays in php. For more information, please follow other related articles on the PHP Chinese website!