value 1, key 2 => value 2,..., key N => value N);"; 3. Use array literals Use the quantity "[]" to create an array and assign a value, the syntax is "$array variable name = [key 1 => value 1, key 2 => value 2,..., key N => value N];"."/> value 1, key 2 => value 2,..., key N => value N);"; 3. Use array literals Use the quantity "[]" to create an array and assign a value, the syntax is "$array variable name = [key 1 => value 1, key 2 => value 2,..., key N => value N];".">
Home >Backend Development >PHP Problem >There are several ways to create a one-dimensional array in PHP and assign values
There are three ways: 1. Direct assignment to create an array and assign the value, the syntax is "$array variable name [subscript] = value;", where the subscript (key name) can be a string or An integer; 2. Use array() to create an array and assign a value. The syntax is "$array variable name=array(key 1=> value 1, key 2 => value 2,..., key N=> value N );"; 3. Use the array literal "[]" to create an array and assign a value. The syntax is "$array variable name=[key 1=> value 1, key 2 => value 2,..., key N= >Value N];".
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
When each element in the array is a specific When the value is rather than an array, we call such an array a one-dimensional array. One-dimensional arrays are the simplest and most commonly used arrays.
In PHP, there are three ways to create a one-dimensional array and assign values.
Method 1: Create a one-dimensional array by direct assignment
The syntax format for declaring a one-dimensional array using the method of direct assignment to array elements is as follows:
$数组变量名[下标] = 值;
The subscript (index value) can be a string or an integer, and the subscript needs to be wrapped with [].
The sample code is as follows:
<?php header("Content-type:text/html;charset=utf-8"); $array[0] = '苹果'; $array[1] = '香蕉'; $array[2] = '橘子'; $array[3] = '榴莲'; var_dump($array); ?>
The running results are as follows:
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:
<?php header("Content-type:text/html;charset=utf-8"); $array[0] = '苹果'; $array[1] = '香蕉'; $array[2] = '橘子'; $array[3] = '榴莲'; echo '$array[0] = ' . $array[0] . '<br>'; echo '$array[1] = ' . $array[1] . '<br>'; echo '$array[2] = ' . $array[2] . '<br>'; echo '$array[3] = ' . $array[3] . '<br>'; ?>
The running results are as follows:
Note: When declaring an index array, if the index value is increasing, we can also not specify the specific index value within the square brackets. In this case, the index value will increase sequentially from 0 by default.
Method 2: Use the array() function to create a one-dimensional array
Another way to declare an array 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:
<?php header("Content-type:text/html;charset=utf-8"); $array = array(0 => '红色', 1 => '黄色', 2 => '蓝色', 3 => '紫色'); echo '<pre class="brush:php;toolbar:false">'; var_dump($array); ?>
The running result is as follows:
If you do not use = The >
symbol specifies the subscript, and the default is the index array. The default index value also starts from 0 and increases in sequence. The sample code is as follows:
<?php header("Content-type:text/html;charset=utf-8"); $array = array('红色','黄色','蓝色', '紫色'); var_dump($array); ?>
The running results are the same as those of the previous example.
Method 3: Use array literal "[]" to create a one-dimensional array
Array literals (Array Literals), Is a comma-separated list containing one or more expressions, enclosed in square brackets ([…]).
It accepts a certain number of key=>value parameter pairs separated by commas. The syntax format is as follows:
$数组变量名 = [key1 => value1, key2 => value2, ..., keyN => valueN];
The sample code is as follows:
<?php header("Content-type:text/html;charset=utf-8"); $array = [1=>"1","a"=>"",2=>"2","b"=>0,"c"=>"blue"]; var_dump($array); ?>
If you do not use the =>
symbol to specify the subscript, The default is an indexed array. The default index value also starts from 0 and increases in sequence. The sample code is as follows:
<?php header("Content-type:text/html;charset=utf-8"); $array = ['红色','黄色','蓝色', '紫色']; var_dump($array); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of There are several ways to create a one-dimensional array in PHP and assign values. For more information, please follow other related articles on the PHP Chinese website!