Home Backend Development PHP Problem What are the two ways to assign values ​​to php arrays?

What are the two ways to assign values ​​to php arrays?

Apr 12, 2023 pm 01:53 PM

In PHP, there are two conventional array assignment methods:

1. Use the "array()" function to create an array

In PHP, create an empty array or an array with For arrays of elements, we often use the array() function. The syntax is as follows:

$array_var_name = array();
Copy after login

Alternatively, we can add elements directly within the array function, the syntax is as follows:

$array_var_name = array('元素1', '元素2', '元素3');
Copy after login

Note: When using the array() function to declare an array, if within the square brackets "[ ]" omit the array subscript, the PHP engine will automatically assign consecutive integer subscripts starting from 0 to the array elements. The sample code is as follows:

$array = array('Apple', 'Orange', 'Banana'); 
// 等价于
$array[0] = 'Apple';
$array[1] = 'Orange';
$array[2] = 'Banana';
Copy after login

2. Use the "[]" operator to assign array elements

In addition to using the array() function, PHP also provides the "[]" operator, which can be used Add new elements to the array. The sample code is as follows:

$array_var_name = []; // 空数组
$array_var_name[] = '元素1'; 
$array_var_name[] = '元素2'; 
$array_var_name[] = '元素3';
Copy after login

is equivalent to

$array_var_name = array();
$array_var_name[] = '元素1'; 
$array_var_name[] = '元素2'; 
$array_var_name[] = '元素3';
Copy after login

Note: When using the "[]" method to add new elements to the array, PHP will automatically add elements to the next consecutive position in the array. The premise is that the subscript of the new element is not specified! If a subscript is specified, it is added to the specified position. The sample code is as follows:

$a = array('a', 'b', 'c'); 
$a[8] = 'z'; // 或者 $a['key'] ='z';
var_dump($a); 
//输出
//array(4) {
//  [0]=>
//  string(1) "a"
//  [1]=>
//  string(1) "b"
//  [2]=>
//  string(1) "c"
//  [8]=>
//  string(1) "z"
//}
Copy after login

In addition to these two methods, PHP also provides many array-related operation functions and methods, such as "array_pop()", "array_push()", "array_shift()", " array_unshift()" and so on. Mastering these functions can make your PHP development more efficient and convenient!

The above is the detailed content of What are the two ways to assign values ​​to php arrays?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24