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 several ways to create a one-dimensional array in PHP and assign values

青灯夜游
青灯夜游Original
2022-08-18 18:04:383540browse

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];".

There are several ways to create a one-dimensional array in PHP and assign values

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] = &#39;苹果&#39;;
$array[1] = &#39;香蕉&#39;;
$array[2] = &#39;橘子&#39;;
$array[3] = &#39;榴莲&#39;;
var_dump($array);
?>

The running results are as follows:

There are several ways to create a one-dimensional array in PHP and assign values

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] = &#39;苹果&#39;;
$array[1] = &#39;香蕉&#39;;
$array[2] = &#39;橘子&#39;;
$array[3] = &#39;榴莲&#39;;
echo &#39;$array[0] = &#39; . $array[0] . &#39;<br>&#39;;
echo &#39;$array[1] = &#39; . $array[1] . &#39;<br>&#39;;
echo &#39;$array[2] = &#39; . $array[2] . &#39;<br>&#39;;
echo &#39;$array[3] = &#39; . $array[3] . &#39;<br>&#39;;
?>

The running results are as follows:

There are several ways to create a one-dimensional array in PHP and assign values

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 => &#39;红色&#39;, 1 => &#39;黄色&#39;, 2 => &#39;蓝色&#39;, 3 => &#39;紫色&#39;);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
var_dump($array);
?>

The running result is as follows:

There are several ways to create a one-dimensional array in PHP and assign values

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(&#39;红色&#39;,&#39;黄色&#39;,&#39;蓝色&#39;, &#39;紫色&#39;);
var_dump($array);
?>

The running results are the same as those of the previous example.

There are several ways to create a one-dimensional array in PHP and assign values

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);
?>

There are several ways to create a one-dimensional array in PHP and assign values

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 = [&#39;红色&#39;,&#39;黄色&#39;,&#39;蓝色&#39;, &#39;紫色&#39;];
var_dump($array);
?>

There are several ways to create a one-dimensional array in PHP and assign values

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn