Home  >  Article  >  Backend Development  >  How to delete numeric subscripts from php arrays

How to delete numeric subscripts from php arrays

PHPz
PHPzOriginal
2023-04-26 09:10:46536browse

In PHP, an array is a powerful data structure that can store different types of data. Each element in the array has a corresponding key and value. Keys can be strings or numbers, and arrays can be single or multidimensional. When working with arrays, we may need to delete one or more elements. In this article, you will learn how to remove numeric subscripts from PHP arrays.

In PHP, there are different ways to delete numerically subscripted array elements, depending on whether we want to delete one element or multiple elements. Let's cover both cases separately.

Delete a single element

To delete a single element, we can use the unset function. This function takes an array and key as arguments and removes the specified key/value pair from the array. Following is the sample code to delete an array element with numeric subscript:

<?php
// 声明一个整数数组
$numbers = array(0, 1, 2, 3, 4, 5);

// 删除索引为2的元素
unset($numbers[2]);

// 输出数组
var_dump($numbers);
?>

In the above code, we have declared an array containing six integers and deleted the element with key 2. When using the unset function to delete array elements, remember that if the array uses consecutive numeric subscripts, the elements will be re-indexed using the new numbers.

If we want to delete all the elements in the array that have a specific value, we can use the array_search and unset functions. The array_search function is used to find the key of a specific value in an array. Here is a sample code to remove a specific element from an array:

<?php
// 声明一个字符串数组
$names = array(&#39;Tom&#39;, &#39;Susan&#39;, &#39;John&#39;, &#39;Sara&#39;);

// 查找&#39;John&#39;的键
$key = array_search(&#39;John&#39;, $names);

// 删除找到的元素
unset($names[$key]);

// 输出数组
var_dump($names);
?>

In the above code, we have created an array of strings and used array_search to find the key of a specific value. Then we use unset to remove the found elements. Finally, we use the var_dump function to output the results.

Delete multiple elements

To delete multiple elements, we can use the array_splice function. This function removes the element at the specified offset and length and moves the other elements of the array to the new position. Following is the sample code to delete array elements with numeric subscript:

<?php
// 声明一个字符串数组
$names = array(&#39;Tom&#39;, &#39;Susan&#39;, &#39;John&#39;, &#39;Sara&#39;);

// 从键为1的元素开始,删除两个元素
array_splice($names, 1, 2);

// 输出数组
var_dump($names);
?>

In the above code, we have created an array of strings and used array_splice function to delete two elements from the array, starting with key 1 The elements start counting. Finally, we use the var_dump function to output the results.

Conclusion

In PHP, we can delete array elements with numeric subscripts using the unset, array_search and array_splice functions. If we want to remove a single element we can use the unset function and if we want to remove multiple elements we can use the array_splice function. Understanding these functions can make working with PHP arrays easier and more efficient.

The above is the detailed content of How to delete numeric subscripts from php arrays. 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