array_pop 函數 官方說明:
mixed array_pop ( array &$array )
array_pop() pops and returns the last value of the array, shortening the array by one element. If array is empty (or is not an array), NULL will be returned. Will additionally produce a Warning when called on a non-array.
Note: This function will reset() the array pointer after use.
Parameters
array:The array to get the value from.
Return Values:
Returns the last value of array. If array is empty (or is not an array), NULL will be returned.
測試代碼如下:
/**
* Tests array_pop()
*/
public function testArray_pop() {
// array_pop(/* parameters */);
$stack = array ("orange", "banana", "apple", "raspberry" );
$fruit = array_pop ( $stack );
print_r ( $stack );
$this->assertArrayNotHasKey("raspberry",$stack,"$stack is still has the key \'raspberry\'");
$stack_null =null;
array_pop($stack_null);
$array_empty = array();
$this->assertNull(array_pop($array_empty),"function array_pop($array_empty) return not null");
print_r($array_empty);
}
測試結果如下:
Content-type: text/html
Array
(
[0] => orange
[1] => banana
[2] => apple
)
Warning: array_pop(): The argument should be an array in D:\php\phpworkspace\php\array_popTest.php on line 48
Array
(
)