Home >Backend Development >PHP Tutorial >PHP deletes the last element of the array array_pop() function usage_PHP tutorial

PHP deletes the last element of the array array_pop() function usage_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:59:441088browse

Deleting an element in an array is the most common. Today I will briefly introduce how to delete the last element. If you want to delete any element in the array, you can use my second method and add an if ( ){ unset()} can be achieved.

Example

The code is as follows
 代码如下 复制代码

$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_pop($stack);
print_r($stack);
?>
经过此操作后,$stack 将只有 3 个单元:

Array
(
    [0] => orange
    [1] => banana
    [2] => apple
)

Copy code


$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_pop($stack);

print_r($stack);
代码如下 复制代码

$array = array('ccc','www.hzhuti.com','abc','bKjia.c0m');
$arrtmp = array();
for( $i=0;$i {
$arrtmp[] = $array[$i];
}
print_r($arrtmp);

Array
(
[0] => ccc
    [1] => www.hzhuti.com
    [2] => abc
)

?>

?> After this operation, $stack will only have 3 cells: Array (

[0] => orange

[1] => banana ) array_pop() pops and returns the last element of array and decrements the length of array by one. If array is empty (or not an array) NULL will be returned After using functions, we can also use other methods such as
The code is as follows Copy code
$array = array('ccc','www.hzhuti.com','abc','bKjia.c0m');<🎜> $arrtmp = array();<🎜> for( $i=0;$i {<🎜> $arrtmp[] = $array[$i];<🎜> }<🎜> print_r($arrtmp);<🎜> <🎜>Array<🎜> (<🎜> [0] => ccc [1] => www.hzhuti.com [2] => abc ) ?> http://www.bkjia.com/PHPjc/631309.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631309.htmlTechArticleDeleting an element in an array is the most common. Today we will only briefly introduce how to delete the last element. , if you want to delete any one in the array, you can use my second...
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