PHP declares array to modify elements

王林
Release: 2023-05-07 19:13:05
Original
414 people have browsed it

Overview

In PHP, an array is a very common data type that can store multiple values, and the data can be accessed using indexes or associated keys. Modifying elements in an array is a very common operation. This article will introduce how to declare an array and modify array elements in PHP.

Declaring arrays

In PHP, there are three ways to declare arrays.

1. Use the array() function

Using the array() function is the most common method. Its syntax is as follows:

$array = array(value1, value2, value3, ...);
Copy after login

where value1, value2, value3, etc. are The elements in the array can be of any data type, including numbers, strings, Boolean values, objects, etc. At the same time, we can also use key names to specify key-value pairs when declaring an array, as shown below:

$array = array(
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3'
); 
Copy after login

The array declared in this way is an associative array, and each element has a unique key name.

2. Use square brackets

In addition to using the array() function, we can also use square brackets [] to declare an array. The syntax is as follows:

$array = [value1, value2, value3, ...];
Copy after login

Can also be used in Use key names in square brackets to specify key-value pairs, as shown below:

$array = [
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3'
];
Copy after login

3. Declare an empty array

Sometimes we want to declare an empty array first, and then add elements step by step. In PHP, there are two ways to declare an empty array: using the array() function and using square brackets []. Their syntax is as follows:

//使用array()函数
$array = array();

//使用方括号
$array = [];
Copy after login

Modify array elements

In PHP, we can use the key name or index of the array to access the array elements and modify them.

1. Use key names

If the array is an associative array, we can modify the value of the element by specifying the key name. The syntax is as follows:

$array['key'] = 'new value';
Copy after login

This line of code will associate The value of the element with key 'key' in array $array is modified to 'new value'.

If the array is an indexed array, we can also use the key name to modify the value of its element. The syntax is as follows:

$array[0] = 'new value';
Copy after login

This line of code will index the value of the first element in the array $array Modify to 'new value'.

2. Use index

For indexed arrays, we can also use the index to modify the value of the element. The syntax is as follows:

$array[index] = 'new value';
Copy after login

where index is the index value of the element to be modified, which can be an integer or variable, and 'new value' is the value to which the element is to be modified.

Example

We can use the following sample code to demonstrate how to declare an array and modify elements:

//使用array()函数声明关联数组
$array1 = array(
    'name' => '张三',
    'age' => 20,
    'gender' => '男'
);

//使用方括号声明索引数组
$array2 = ['apple', 'banana', 'orange'];

//声明空数组
$array3 = []; 

//修改关联数组中的元素
$array1['age'] = 22;

//输出修改后的关联数组
echo '关联数组修改后:';
print_r($array1);

//修改索引数组中的元素
$array2[1] = 'pear';

//输出修改后的索引数组
echo '索引数组修改后:';
print_r($array2);
Copy after login

The output of the above code is as follows:

关联数组修改后:Array
(
    [name] => 张三
    [age] => 22
    [gender] => 男
)
索引数组修改后:Array
(
    [0] => apple
    [1] => pear
    [2] => orange
)
Copy after login

Conclusion

In PHP, declaring arrays and modifying array elements are very basic operations, and they are also very important skills. This article introduces three methods of declaring arrays in PHP, and demonstrates how to use the key name or index of the array to modify elements. To be proficient in these skills requires continuous practice, and I believe readers will gain something from practice.

The above is the detailed content of PHP declares array to modify elements. For more information, please follow other related articles on the PHP Chinese website!

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