What are the ways to create arrays in php

青灯夜游
Release: 2023-03-11 09:58:02
Original
6302 people have browsed it

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

What are the ways to create arrays in php

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:

$数组变量名[下标] = 值
Copy after login

The subscript (index value) can be a string or an integer, and the subscript Requires the [ ] package.

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;<pre class="brush:php;toolbar:false">&#39;;
var_dump($array);
?>
Copy after login

The running results are as follows:

array (size=4)
  0 => string &#39;苹果&#39; (length=6)
  1 => string &#39;香蕉&#39; (length=6)
  2 => string &#39;橘子&#39; (length=6)
  3 => string &#39;榴莲&#39; (length=6)
Copy after login

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:

<?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;;
?>
Copy after login

The running result is as follows:

$array[0] = 苹果
$array[1] = 香蕉
$array[2] = 橘子
$array[3] = 榴莲
Copy after login

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:

<?php
header("Content-type:text/html;charset=utf-8");
$array[] = &#39;苹果&#39;;
$array[] = &#39;香蕉&#39;;
$array[] = &#39;橘子&#39;;
$array[] = &#39;榴莲&#39;;
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
var_dump($array);
?>
Copy after login

The running result is as follows:

array (size=4)
  0 => string &#39;苹果&#39; (length=6)
  1 => string &#39;香蕉&#39; (length=6)
  2 => string &#39;橘子&#39; (length=6)
  3 => string &#39;榴莲&#39; (length=6)
Copy after login

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);
Copy after login

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);
?>
Copy after login

The running result is as follows:

array (size=4)
  0 => string &#39;红色&#39; (length=6)
  1 => string &#39;黄色&#39; (length=6)
  2 => string &#39;蓝色&#39; (length=6)
  3 => string &#39;紫色&#39; (length=6)
Copy after login
Copy after login

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:

<?php
header("Content-type:text/html;charset=utf-8");
$array = array(&#39;红色&#39;,&#39;黄色&#39;,&#39;蓝色&#39;, &#39;紫色&#39;);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
var_dump($array);
?>
Copy after login

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

array (size=4)
  0 => string &#39;红色&#39; (length=6)
  1 => string &#39;黄色&#39; (length=6)
  2 => string &#39;蓝色&#39; (length=6)
  3 => string &#39;紫色&#39; (length=6)
Copy after login
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!