PHP array_pop() 移除元素
array_pop() 函數用來移除並回傳陣列的最後一個元素。
基本用法
<?php
$arr = [1, 2, 3, 4, 5];
$last = array_pop($arr);
echo $last; // 5
print_r($arr); // [1, 2, 3, 4]
?>
語法
array_pop(array &$array): mixed
$array:要修改的陣列(傳址)- 回傳值:被移除的元素,陣列為空時回傳
null
空陣列
<?php
$arr = [];
$result = array_pop($arr);
var_dump($result); // NULL
?>
關聯陣列
<?php
$arr = [
'a' => 1,
'b' => 2,
'c' => 3
];
$last = array_pop($arr);
echo $last; // 3
print_r($arr); // ['a' => 1, 'b' => 2]
?>
堆疊操作 (Stack)
array_push() 和 array_pop() 可以實作 LIFO(後進先出)堆疊:
<?php
$stack = [];
// Push
$stack[] = 'First';
$stack[] = 'Second';
$stack[] = 'Third';
print_r($stack); // ['First', 'Second', 'Third']
// Pop
echo array_pop($stack); // Third
echo array_pop($stack); // Second
echo array_pop($stack); // First
?>
相關函數
array_shift() - 從開頭移除
<?php
$arr = [1, 2, 3, 4, 5];
$first = array_shift($arr);
echo $first; // 1
print_r($arr); // [2, 3, 4, 5]
?>
array_unshift() - 新增到開頭
<?php
$arr = [2, 3, 4];
array_unshift($arr, 1);
print_r($arr); // [1, 2, 3, 4]
?>
佇列操作 (Queue)
使用 array_push() 和 array_shift() 可以實作 FIFO(先進先出)佇列:
<?php
$queue = [];
// 入隊(從尾端加入)
$queue[] = 'First';
$queue[] = 'Second';
$queue[] = 'Third';
// 出隊(從開頭移除)
echo array_shift($queue); // First
echo array_shift($queue); // Second
echo array_shift($queue); // Third
?>
常見應用
處理路徑
<?php
$path = '/home/user/documents/file.txt';
$parts = explode('/', $path);
// 取得檔名
$filename = array_pop($parts);
echo $filename; // file.txt
// 取得目錄
$dir = implode('/', $parts);
echo $dir; // /home/user/documents
?>
復原操作 (Undo)
<?php
class Editor {
private string $content = '';
private array $history = [];
public function write(string $text): void {
$this->history[] = $this->content; // 儲存歷史
$this->content .= $text;
}
public function undo(): void {
if (!empty($this->history)) {
$this->content = array_pop($this->history);
}
}
public function getContent(): string {
return $this->content;
}
}
$editor = new Editor();
$editor->write('Hello');
$editor->write(' World');
echo $editor->getContent(); // Hello World
$editor->undo();
echo $editor->getContent(); // Hello
$editor->undo();
echo $editor->getContent(); // (空)
?>
麵包屑導航
<?php
$breadcrumbs = ['Home', 'Products', 'Electronics', 'Phones'];
// 顯示麵包屑
echo implode(' > ', $breadcrumbs);
// Home > Products > Electronics > Phones
// 返回上一層
array_pop($breadcrumbs);
echo implode(' > ', $breadcrumbs);
// Home > Products > Electronics
?>
處理巢狀結構
<?php
function getNestedValue(array $data, string $path) {
$keys = explode('.', $path);
$value = $data;
foreach ($keys as $key) {
if (!isset($value[$key])) {
return null;
}
$value = $value[$key];
}
return $value;
}
$config = [
'database' => [
'mysql' => [
'host' => 'localhost',
'port' => 3306
]
]
];
echo getNestedValue($config, 'database.mysql.host'); // localhost
?>
安全地取得最後元素
<?php
function getLastElement(array $arr) {
if (empty($arr)) {
return null;
}
// 不修改原陣列
return end($arr);
}
// 或者
function getLastElementSafe(array $arr) {
return $arr[array_key_last($arr)] ?? null; // PHP 7.3+
}
$arr = [1, 2, 3];
echo getLastElement($arr); // 3
print_r($arr); // [1, 2, 3](不變)
?>
效能考量
<?php
// array_pop 是 O(1) 操作
$arr = range(1, 1000000);
$start = microtime(true);
array_pop($arr);
$time1 = microtime(true) - $start;
// array_shift 是 O(n) 操作(需要重新索引)
$arr = range(1, 1000000);
$start = microtime(true);
array_shift($arr);
$time2 = microtime(true) - $start;
echo "array_pop: {$time1}s\n"; // 很快
echo "array_shift: {$time2}s\n"; // 較慢
?>
取得但不移除
如果只想取得最後一個元素但不移除:
<?php
$arr = [1, 2, 3, 4, 5];
// 使用 end()
$last = end($arr);
echo $last; // 5
// PHP 7.3+:使用 array_key_last()
$last = $arr[array_key_last($arr)];
echo $last; // 5
// 使用負數索引(只對數字索引有效)
$last = $arr[count($arr) - 1];
echo $last; // 5
print_r($arr); // [1, 2, 3, 4, 5](不變)
?>
注意事項
<?php
// array_pop 會修改原陣列
$arr = [1, 2, 3];
array_pop($arr);
print_r($arr); // [1, 2]
// 內部指標會被重置
$arr = [1, 2, 3];
next($arr);
echo current($arr); // 2
array_pop($arr);
echo current($arr); // 1(指標被重置)
?>