How to create and initialize a PHP array

PHPz
Release: 2023-09-05 14:04:02
Original
1342 people have browsed it

如何创建和初始化一个 PHP 数组

How to create and initialize a PHP array

PHP array is a very common and useful data structure that can be used to store and organize large amounts of data. Creating and initializing a PHP array is one of the common operations when we write PHP programs. This article will show you how to create and initialize a PHP array, and provide some code examples.

Create an empty array
In PHP, we can use the array keyword array() or directly use empty brackets [] to create an empty array. Here are examples of both methods:

// 使用 array() 创建空数组
$array1 = array();

// 使用 [] 创建空数组
$array2 = [];
Copy after login

Create an array with default values
In addition to creating an empty array, we can also create an array with default values ​​where each element is initialized to the same value. We can use the array_fill() function to achieve this goal. Here is an example:

// 创建一个包含 5 个元素的数组,每个元素初始化为默认值 0
$array = array_fill(0, 5, 0);

// 打印数组
print_r($array);
Copy after login

Output result:

Array
(
    [0] => 0
    [1] => 0
    [2] => 0
    [3] => 0
    [4] => 0
)
Copy after login

Initialize an array of known elements
Sometimes, we already know the elements to be stored in the array and want to Add them to the array all at once. You can use array() or [] syntax to achieve this goal. Here is an example:

// 初始化一个包含多个元素的数组
$array = array('apple', 'banana', 'orange');

// 打印数组
print_r($array);
Copy after login

Output results:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
)
Copy after login

Create an associative array
An associative array is an array that uses string keys to access values. To create an associative array, we can use the array() or [] syntax and use the => symbol to separate key-value pairs. The following is an example:

// 创建一个关联数组
$array = array(
    'name' => 'John Doe',
    'age' => 30,
    'email' => 'john@example.com'
);

// 打印数组
print_r($array);
Copy after login

Output result:

Array
(
    [name] => John Doe
    [age] => 30
    [email] => john@example.com
)
Copy after login

Dynamicly add elements to the array
In addition to initializing the array elements when creating, we can also dynamically add elements to the array when it already exists. Add elements to the array. Elements can be added using the array's built-in function array_push() or directly by assignment. The following are two examples:

// 使用 array_push() 添加元素
$array = array('apple', 'banana');
array_push($array, 'orange');

// 直接通过赋值方式添加元素
$array[] = 'grape';

// 打印数组
print_r($array);
Copy after login

Output results:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => grape
)
Copy after login

Summary
Creating and initializing a PHP array is one of the common operations in writing PHP programs. By using array() or [] syntax, we can easily create and initialize an empty array, an array with default values, an array of known elements, and an associative array. In addition, we can also use the built-in function array_push() or directly assign elements to dynamically add elements to the array. I hope the code examples in this article are helpful when working with PHP arrays.

The above is the detailed content of How to create and initialize a PHP array. 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