In PHP, array is a very common data type. For one-dimensional arrays, sometimes we need to delete or filter some elements in it to achieve the desired effect. This article will introduce how to remove elements from a one-dimensional array in PHP.
1. Delete the element with the specified key name
PHP provides the unset() function, which can be used to delete the element with the specified key name in the array. The following is an example:
$arr = array('a' => 'apple', 'b' => 'banana', 'c' => 'cherry'); unset($arr['b']); //删除键名为'b'的元素 print_r($arr);
The result is:
Array ( [a] => apple [c] => cherry )
In this example, we first define an associative array $arr, which contains three elements. Then use the unset() function to delete the element with the key name 'b', and finally use the print_r() function to output the array. You can see that the specified element has been successfully deleted.
If the key name we want to delete is uncertain, we can use a foreach loop to traverse the array, and then use the unset() function in the loop body to delete the elements that meet the conditions. The following is an example:
$arr = array('apple', 'banana', 'cherry'); foreach ($arr as $key => $value) { if ($value == 'banana') { unset($arr[$key]); //删除值为'banana'的元素 } } print_r($arr);
The result is:
Array ( [0] => apple [2] => cherry )
In this example, we define a normal array $arr, and then use a foreach loop to traverse the array. In the loop body, determine whether the value of the current element is equal to 'banana', and if so, use the unset() function to delete the element.
2. Filter the elements in the array
If we want to retain the elements of the specified condition in the array and delete other elements, we can use the array_filter() function. The first parameter of this function is the array that needs to be filtered, and the second parameter is a callback function used to determine whether the element needs to be retained. The callback function accepts each array element as a parameter and returns a Boolean value to determine whether the element needs to be retained.
The following is an example:
$arr = array(3, 2, 1, 4, 5); $result = array_filter($arr, function ($value) { return $value > 2; //保留大于2的元素 }); print_r($result);
The result is:
Array ( [0] => 3 [3] => 4 [4] => 5 )
In this example, we define a normal array $arr and use the array_filter() function to filter out items greater than 2 elements, and save the result in $result, and finally use the print_r() function to output it.
3. Use the array_splice() function to delete elements
In addition to using the unset() function, you can also use the array_splice() function to delete elements in a one-dimensional array. The first parameter of this function is the original array, the second parameter is the position where deletion starts, and the third parameter is the number of elements to be deleted. The following is an example:
$arr = array('a', 'b', 'c', 'd'); array_splice($arr, 1, 2); //从位置1开始删除2个元素 print_r($arr);
The result is:
Array ( [0] => a [3] => d )
In this example, we define a normal array $arr and use the array_splice() function to delete 2 elements starting from position 1, Finally, use the print_r() function to output.
4. Summary
Through the introduction of this article, we have learned several methods to remove elements from one-dimensional arrays in PHP. Whether it is deleting elements with specified key names, filtering elements in an array, or using the array_splice() function to delete elements, we can easily achieve the functions we need. Depending on the specific application scenario, we can choose the corresponding method to implement it.
The above is the detailed content of PHP removes one-dimensional array. For more information, please follow other related articles on the PHP Chinese website!