Home >Backend Development >PHP Tutorial >How to implement php unset to delete array variables_PHP tutorial
To delete data and variables, you can use unset in PHP. Of course, you can also use empty processing, but destroy the variables and let the PHP variables disappear in the memory.
To delete data and variables, you can use unset in the PHP tutorial. Of course, you can also use empty processing, but destroy the variables and let the PHP variables disappear in the memory.
*/
$array = array('aa'=>1,'bb'=>2);
function delete(&$array, $key)
{
if (!is_array($key)) {
$key = array($key);
}
foreach ($key as $k) {
unset($array[$k]);
}
$array = array_values($array);
}
print_r(array_values($array));print_r(delete($array,'aa'));
/*
unset();
This function allows deleting keys in an array. Be aware that the array will not be reindexed.
$a = array( 1 => 'one', 2 => 'two', 3 => 'three' );
unset( $a[2] );
/* will generate an array, defined as
$a = array( 1=>'one', 3=>'three');
instead of
$a = array( 1 => 'one', 2 => 'three');
Clear the entire array
unset($arr);
4: Clear the specified element
unset($arr[index]);
unset() destroys the specified variable. Note that in php 3, unset() will return true (actually the integer value 1),
In PHP 4, unset() is no longer a real function: it is now a statement