Home  >  Article  >  Backend Development  >  php remove array add value

php remove array add value

PHPz
PHPzOriginal
2023-05-06 09:57:07476browse

php is a very popular programming language, especially in web development. It is very popular among developers because of its ease of learning, ease of use, and high flexibility. In PHP, array is a very commonly used data type, and when operating on an array, it is often necessary to add, delete, modify, and check elements in it. This article will introduce how to remove elements from an array and add new values.

1. Remove elements from an array

There are many ways to remove elements from an array in PHP. The following are three commonly used methods:

1. Use unset() function

The unset() function can be used to destroy the specified variable, so it can also be used to delete elements in the array. An example is as follows:

$arr = array('apple', 'banana', 'cherry');
unset($arr[1]);        //删除数组中下标为1的元素banana
print_r($arr);         //输出结果为:Array ( [0] => apple [2] => cherry )

2. Use array_splice() function

array_splice() function can be used to delete a continuous element in the array, and can insert new elements into the array after deletion Location. The syntax of this function is:

array_splice(array &$input , int $offset [, int $length = 0 [, mixed $replacement ]]): array

where $input is the array to be operated on, $offset is the starting subscript of the element to be deleted, and $length is the number of elements to be deleted. $replacement is an optional parameter and represents the new element to be inserted. An example is as follows:

//删除下标为1的元素banana,并插入新元素orange
$arr = array('apple', 'banana', 'cherry');
array_splice($arr, 1, 1, array('orange'));
print_r($arr);         //输出结果为:Array ( [0] => apple [1] => orange [2] => cherry )

3. Use array_filter() function

array_filter() function can filter the elements in the array and return the filtered new array. When you need to delete certain elements in the array, you can define the unnecessary elements as filter conditions and use this function to filter. Examples are as follows:

//删除值为banana、cherry的元素
$arr = array('apple', 'banana', 'cherry');
$arr = array_filter($arr, function($value){
    return $value != 'banana' && $value != 'cherry';
});
print_r($arr);         //输出结果为:Array ( [0] => apple )

2. Add elements to an array

In PHP, there are many ways to add elements to an array. The following are two commonly used methods:

1. Use the [] operator

Use the [] operator to add new elements at the end of the array. An example is as follows:

$arr = array('apple', 'banana', 'cherry');
$arr[] = 'orange';      //在数组末尾添加新元素orange
print_r($arr);         //输出结果为:Array ( [0] => apple [1] => banana [2] => cherry [3] => orange )

2. Use array_push() function

array_push() function can add one or more elements to the end of the array and return the new length of the array. An example is as follows:

//在数组末尾添加新元素orange
$arr = array('apple', 'banana', 'cherry');
array_push($arr, 'orange');
print_r($arr);         //输出结果为:Array ( [0] => apple [1] => banana [2] => cherry [3] => orange )

Summary

According to the above introduction, it can be seen that there are many ways to remove elements from an array and add new values ​​in PHP, and their specific use depends on actual needs. and operating habits. When you need to delete elements in the array, you can choose to use the unset() function, array_splice() function or array_filter() function; when you need to add new elements, you can use the [] operator or array_push() function. It should be noted that when using these methods, the corresponding function should be selected according to the specific situation to achieve optimal results and code readability.

The above is the detailed content of php remove array add value. 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