PHP 數組以其動態、有序、鍵值共存的特性,成為處理批量數據的利器。不同於 C/Java 需預定長度,PHP 數組可隨意擴展。本文通過模擬「麻將洗牌發牌」這一經典場景,展示數組初始化、隨機打亂、多維切片等核心操作。
<?php
$majiang = []; // PHP 5.4+ 短語法
$majiang[] = '0'; // 佔位,索引從 1 開始
for ($suit = 1; $suit <= 3; $suit++) { // 花色
for ($rank = 1; $rank <= 9; $rank++) { // 點數
for ($copy = 1; $copy <= 4; $copy++) { // 4 張相同牌
$majiang[] = "{$suit}_{$rank}.gif"; // 對應圖片文件名
}
}
}
// 此時 $majiang[1..108] 為有序牌庫
?>
function shuffle_mahjong(&$deck, $times = 1000) {
$n = count($deck) - 1; // 排除索引 0
for ($i = 0; $i < $times; $i++) {
$r1 = mt_rand(1, $n);
$r2 = mt_rand(1, $n);
// 交換
[$deck[$r1], $deck[$r2]] = [$deck[$r2], $deck[$r1]];
}
}
shuffle_mahjong($majiang, 2000); // 充分打亂
優化提示:PHP 內建 shuffle() 已實現 Fisher-Yates,生產代碼請直接使用 shuffle($majiang),效能更優且無偏差。
$players = [1 => [], 2 => [], 3 => [], 4 => []];
$seek = 1; // 發牌指針
// 三輪:每人 4 張
for ($round = 1; $round <= 3; $round++) {
for ($seat = 1; $seat <= 4; $seat++) {
for ($c = 1; $c <= 4; $c++) {
$players[$seat][] = $majiang[$seek++];
}
}
}
// 一輪:每人 1 張
for ($seat = 1; $seat <= 4; $seat++) {
$players[$seat][] = $majiang[$seek++];
}
// 莊家(座位 4)再摸 1 張
$players[4][] = $majiang[$seek];
// 驗證:4×13 + 14 = 66 張,seek 應為 67
echo "發牌指針: $seek, 剩餘牌: " . (108 - $seek + 1);
function render_hand($tiles) {
foreach ($tiles as $tile) {
echo '<img src="img/' . htmlspecialchars($tile) . '" alt="">';
}
echo '<br>';
}
foreach ($players as $seat => $hand) {
echo "玩家 $seat (" . count($hand) . "張): ";
render_hand($hand);
}
將規則參數化,支援多地玩法:
class MahjongGame {
public $suits = 3;
public $ranks = 9;
public $copies = 4;
public $players = 4;
public $dealer_extra = 1;
public $rounds = 3;
public $tiles_per_round = 4;
public function generate_deck() { /* ... */ }
public function deal() { /* ... */ }
}
count()/foreach 遍歷,效能最優&$arr 避免大數組複製mt_rand() 優於 rand();加密級需求用 random_int()shuffle()、array_slice()、array_chunk() 替代手寫循環這個例子展示了 PHP 數組處理複雜業務邏輯的表達力。將現實規則映射為數據操作,是程式設計的核心樂趣所在。