How to modify array member variables in PHP

PHPz
Release: 2023-04-19 11:01:03
Original
435 people have browsed it

In PHP development, array is a very commonly used data type, usually used to store large amounts of data to facilitate data operation and management. In practical applications, we may need to modify the member variables of the array. This article will introduce how to modify the array member variables in PHP.

1. Directly modify array member variables through subscripts

Directly modifying array member variables through subscripts is the most basic method in PHP. You only need to specify the subscript of the array member variables to be modified. Can. The sample code is as follows:

$arr = array('apple', 'banana', 'orange');
$arr[1] = 'pear';
print_r($arr);
Copy after login

The output result is:

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

As you can see, in the sample code, the second element of the array $arr is changed through the statement $arr[1] = 'pear'; elements are modified to 'pear', and the modified array is printed.

2. Modify the array member variables through the key value of the array

In PHP, we can use arrays in the form of key-value pairs, that is, associative arrays. Each element in an associative array has a key-value pair, where the key represents the member variable name and the value represents the value of the member variable. It is also very simple to modify the value through the key value, as shown below:

$arr = array('name' => 'Tom', 'age' => 20, 'gender' => 'male');
$arr['age'] = 25;
print_r($arr);
Copy after login

The output result is:

Array
(
    [name] => Tom
    [age] => 25
    [gender] => male
)
Copy after login

As you can see, in the sample code, $arr['age'] = 25; This statement modifies the value of the key 'age' in the associative array $arr to 25, and prints the modified array.

3. Modify array member variables through array methods

PHP also provides many array operation methods, which can easily perform various modification operations on arrays. Here are just a few commonly used methods.

3.1 array_push

array_push can add one or more elements to the end of the array and return the number of added elements. The sample code is as follows:

$arr = array('apple', 'banana', 'orange');
$num = array_push($arr, 'pear');
print_r($arr);
echo '添加了'.$num.'个新元素';
Copy after login

The output result is:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => pear
)
添加了1个新元素
Copy after login

As you can see, the sample code adds an element 'pear' to the end of the array $arr through the array_push method, and prints out the addition the array after.

3.2 array_pop

array_pop can delete the elements at the end of the array and return the deleted value. The sample code is as follows:

$arr = array('apple', 'banana', 'orange');
$val = array_pop($arr);
print_r($arr);
echo '删除了'.$val;
Copy after login

The output result is:

Array
(
    [0] => apple
    [1] => banana
)
删除了orange
Copy after login

As you can see, the sample code uses the array_pop method to delete the element 'orange' at the end of the array $arr, and prints out the deleted array.

3.3 array_shift

array_shift can delete the elements at the beginning of the array and return the deleted value. The sample code is as follows:

$arr = array('apple', 'banana', 'orange');
$val = array_shift($arr);
print_r($arr);
echo '删除了'.$val;
Copy after login

The output result is:

Array
(
    [0] => banana
    [1] => orange
)
删除了apple
Copy after login

As you can see, the sample code uses the array_shift method to delete the element 'apple' at the beginning of the array $arr, and prints out the deleted array.

4. Summary

In PHP, array member variables can be modified through subscripts, key values ​​and array methods. When operating an array, you need to choose an appropriate method according to the specific situation and modify the array elements as needed. In actual development, it is very necessary to master the modification operations of arrays, which can improve development efficiency and code quality.

The above is the detailed content of How to modify array member variables in PHP. 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