Home >Backend Development >PHP Tutorial >How to implement php unset to delete array variables_PHP tutorial

How to implement php unset to delete array variables_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:02:011142browse

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


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445400.htmlTechArticleTo delete data and variables, you can use unset in php. Of course, you can also use empty space. Process, but destroy the variables and let the php variables disappear in the memory. To delete data...
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