本文共 1407 字,大约阅读时间需要 4 分钟。
php unset函数
Given an array and we have to remove an element from the array.
给定一个数组,我们必须从数组中删除一个元素。
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代码从数组中删除元素
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 。
翻译自:
php unset函数
转载地址:http://bctzd.baihongyu.com/