php unset函数_PHP | 使用unset()函数从数组中删除元素
php unset函数
Given an array and we have to remove an element from the array.
给定一个数组,我们必须从数组中删除一个元素。
unset()函数 (unset() function)
To remove an element from an array, we can use a PHP library unset() function, it accepts the index and removes the element exists on the specified index.
要从数组中删除元素 ,我们可以使用PHP库unset()函数 ,该函数接受索引并删除指定索引上存在的元素。
We are also using another function var_dump() – which dumps the variable details i.e. here, it will print the array variable.
我们还使用了另一个函数var_dump() -转储变量的详细信息,即在这里,它将打印数组变量。
PHP code to remove an element from an array
PHP代码从数组中删除元素
<?php //PHP code to remove an element from an array //declaring an array of strings $array = array('the','quick','brown','fox');//printing the array variable var_dump($array);//removing element from 1st index unset ($array[1]);//again, printing the array variable var_dump($array);//assigning the array after removing its element //from 1st index to the new array $array_new=array_values($array);//printing the new array variable var_dump($array_new); ?>Output
输出量
array(4) {[0]=>string(3) "the"[1]=>string(5) "quick" [2]=>string(5) "brown" [3]=>string(3) "fox" } array(3) {[0]=>string(3) "the" [2]=>string(5) "brown" [3]=>string(3) "fox" } array(3) {[0]=>string(3) "the" [1]=>string(5) "brown" [2]=>string(3) "fox" }Explanation:
说明:
Here, We've created an array ($array) and then used the PHP unset() method to remove index 1 (which is the 2nd value since array starts from 0). Once that's removed, we print the array using var_dump but there is a problem that the indexes haven't updated. So, we create $array_new by using array_values() method on the existing $array.
在这里,我们创建了一个数组( $ array ),然后使用PHP unset()方法删除了索引1(这是第二个值,因为array从0开始)。 删除后,我们使用var_dump打印数组,但是存在索引尚未更新的问题。 因此,我们通过在现有$ array上使用array_values()方法创建$ array_new 。
翻译自: https://www.includehelp.com/php/delete-an-element-from-an-array-using-unset-function.aspx
php unset函数
总结
以上是生活随笔为你收集整理的php unset函数_PHP | 使用unset()函数从数组中删除元素的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: c#读取指定字符后的字符_在C#中读取字
- 下一篇: php属于脚本,php是脚本语言吗