Hướng dẫn
Quảng cáo

Hàm array_pop() trong PHP

Hướng dẫn cách sử dụng hàm array_pop() về mảng trong lập trình PHP

Tác dụng của hàm array_pop()

The array_pop() function removes the last element of an array.

The following table summarizes the technical details of this function.

Return Value: Returns the value of the last element of array.
If array is empty or is not an array, NULL will be returned.
Version: PHP 4+

Syntax

The basic syntax of the array_pop() function is given with:

array_pop(array);

The following example shows the array_pop() function in action.

<?php
// Sample array
$cities = array("London", "Paris", "New York", "Sydney");
    
// Remove and get the last value from array
echo array_pop($cities); // Prints: Sydney
print_r($cities);
?>

Parameters

The array_pop() function accepts the following parameters.

Parameter Description
array Required. Specifies the array to get the last value from.

More Examples

Here're some more examples showing how array_pop() function actually works:

This function can also be used with the associative array, as shown here:

<?php
// Sample array
$alphabets = array("a"=>"apple", "b"=>"ball", "c"=>"cat", "d"=>"dog");
    
// Remove and get the last value from array
echo array_pop($alphabets); // Prints: dog
print_r($alphabets);
?>

Bài viết này đã giúp ích cho bạn?

Bài viết mới

Advertisements