Home  >  Article  >  Backend Development  >  How to delete one-dimensional array in php

How to delete one-dimensional array in php

PHPz
PHPzOriginal
2023-04-27 15:37:48717browse

In PHP, one-dimensional array is one of the most common data types. Usually we need to perform operations such as adding, modifying, querying and deleting one-dimensional arrays. This article will focus on how to delete a one-dimensional array in PHP.

There are several points to note when deleting a one-dimensional array:

1. You need to determine the position index of the element to be deleted;

2. After deleting the element, you need to reorder the entire The index position of the array.

In response to the above two problems, PHP provides some convenient functions to help us perform array operations. Below we will explain the usage of these functions in detail.

1. Use the unset function to delete elements

unset is a commonly used function in PHP, which is used to release the memory space of the specified variable. When unset is used to delete an array element, the element's position will be cleared (becomes NULL), but the keys (index positions) of the array will not be reordered.

Look at an example:

<?php
$fruits = array("apple", "banana", "orange", "kiwi");
unset($fruits[1]);  // 删除数组元素"banana"
print_r($fruits);
?>

This code will return the following results:

Array
(
    [0] => apple
    [2] => orange
    [3] => kiwi
)

It can be seen that after deleting the element "banana", the subscript position of the element is cleared. But the keys of the array are not reordered.

2. Use the array_splice function to delete elements

The array_splice function is one of the functions in PHP used to delete, replace, and insert array elements. It supports multiple operations, this article only discusses how to use it to delete array elements.

The array_splice function has 3 parameters:

1.array: the array to be operated on;

2.offset: the position index of the element to be deleted;

3.length: The number of elements to be deleted (default is 1).

Look at an example:

<?php
$fruits = array("apple", "orange", "banana", "kiwi");
array_splice($fruits, 2, 1);  // 删除数组元素"banana"
print_r($fruits);
?>

This code will return the following results:

Array
(
    [0] => apple
    [1] => orange
    [2] => kiwi
)

It can be seen that after deleting the element "banana", the keys of the array have started from 0 The order has been reordered.

3. Use the array_diff function to delete elements

In PHP, there is also a function called array_diff, which can be used to delete elements in an array. The array_diff function can accept multiple array parameters and returns elements that exist in the first array but do not appear in subsequent arrays.

Therefore, if we want to delete an element in the array, we can perform a difference operation between the element and the original array. This method does not need to determine the position index of the element to be deleted, nor does it need to reorder the index position.

Look at an example:

<?php
$fruits = array("apple", "orange", "banana", "kiwi");
$fruits = array_diff($fruits, array("banana"));  // 删除数组元素"banana"
print_r($fruits);
?>

This code will return the following results:

Array
(
    [0] => apple
    [1] => orange
    [3] => kiwi
)

It can be seen that after deleting the element "banana", the keys of the array have started from 0 The order has been reordered.

Summary:

The above are three ways to delete one-dimensional arrays in PHP. Among them, using the unset function and the array_splice function requires determining the position index of the element to be deleted, and reordering the keys (index positions) of the array; while using the array_diff function does not require these operations.

In practical applications, we can choose different methods to delete elements in one-dimensional arrays according to specific needs.

The above is the detailed content of How to delete one-dimensional array in php. 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