value 1, key 2 => value 2,...);"."/> value 1, key 2 => value 2,...);".">

Home  >  Article  >  Backend Development  >  What are the two ways to define arrays in php?

What are the two ways to define arrays in php?

青灯夜游
青灯夜游Original
2022-05-10 15:57:422374browse

Two methods: 1. Directly assign values ​​to array elements. The syntax is "$array variable name [subscript] = value;". The "subscript" can be a string, an integer, or is empty; 2. Use the array() function to create an array, with the syntax "$array variable name = array(key 1 = > value 1, key 2 = > value 2,...);".

What are the two ways to define arrays in php?

The operating environment of this tutorial: Windows 7 system, PHP version 7.1, DELL G3 computer

Defining arrays in PHP is very flexible, unlike other Unlike arrays in many programming languages, PHP does not need to specify the size of the array when creating it, or even declare it before using it. It can also store any type of data in the same array.

PHP can use the following two methods to define an array:

  • Directly assign values ​​to array elements

  • Use the array() function

We will introduce these two methods in detail with our actual code examples below.

1. Directly assign values ​​to array elements

We can use the form "$Array variable name [subscript] = value ;" format to create and initialize the array

The subscript can be a string, an integer, or empty (that is, no specific index value is specified).

Example 1:

<?php
header("Content-type:text/html;charset=utf-8");
$array[0] = &#39;欢迎&#39;;
$array[1] = &#39;来到&#39;;
$array[2] = &#39;PHP中文网&#39;;
$array[&#39;url&#39;] = &#39;https://www.php.cn/&#39;;
//输出语句
var_dump($array);
?>

Output result:

What are the two ways to define arrays in php?

Example 2:

<?php
header("Content-type:text/html;charset=utf-8");
$array[] = &#39;香蕉&#39;;
$array[] = &#39;苹果&#39;;
$array[] = &#39;橘子&#39;;
$array[] = &#39;榴莲&#39;;
//输出语句
var_dump($array);
?>

Output result:

What are the two ways to define arrays in php?

It can be seen that when we do not specify a specific index value within square brackets, the default is a numeric index, and the index value increases sequentially starting from 0 by default.

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 the elements in the 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;PHP中文网&#39;;
$array[&#39;url&#39;] = &#39;https://www.php.cn/&#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[&#39;url&#39;].&#39;<br>&#39;;
?>

What are the two ways to define arrays in php?

2. Use array() function

Another way to define 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);

1) Direct array() function, without adding any parameters, can create an empty array

<?php
$arr = array();
//输出语句
var_dump($arr);
?>

Output:

What are the two ways to define arrays in php?

2) If you do not use the => symbol to specify a subscript, it defaults to an index array. The default index value also starts from 0 and increases in sequence.

<?php
header("Content-type:text/html;charset=utf-8");
$array= array("香蕉","苹果","梨子","橙子","橘子","榴莲");
//输出语句
var_dump($array);
?>

Output:

What are the two ways to define arrays in php?

3) The array() function also accepts a certain number of key=>value# separated by commas ##Parameter pair, this definition is an associative array.

<?php
header("Content-type:text/html;charset=utf-8");
$array= array(0=>"欢迎来到",1=>"php中文网",2=>"PHP教程","URL"=>"https://www.php.cn/");
//输出语句
var_dump($array);
?>

Output result:


What are the two ways to define arrays in php?

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of What are the two ways to define arrays in php?. 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