$a[0] = 123; // 一維
$a[1][0] = 10; // 二維
$a[1][2][3] = 'Brad'; // 三維
$a[2] = array(1, 2, 3, 4, 5);
var_dump($a);
// array(3) {
// [0]=> int(123)
// [1]=> array(2)
// {
// [0]=> int(10)
// [2]=> array(1) { //$a[1][2][3] = 'Brad';
// [3]=> string(4) "Brad"
// }
//
// [2]=> array(5) {
// [0]=> int(1)
// [1]=> int(2)
// [2]=> int(3)
// [3]=> int(4)
// [4]=> int(5)
// }
A:是因為程式是一行一行跑,陣列的特性是門越後面越晚開,他是在$vv被輸出
//key[0][1][2] : value 123、array(2)、array(5)
foreach ($a as $k => $v) {
// $v看第一格[]array進下一層
if (gettype($v) == 'array') {
//key[0][2] : int(10)、array(1)
foreach ($v as $kk => $vv) {
// $vv看第二格[]array進下一層
if (gettype($vv) == 'array') {
// [3]=> string(4)
// $vvv看第三格[]array echo $vvv
foreach ($vv as $kkk => $vvv) {
echo $vvv; // $vvv看第三格[] "Brad"
}
} else {
echo $vv; //$vv看第二格[] int(10)
}
echo '<br />';
}
} else {
echo $v; //$v看第一格[] value 123
}
echo '<br />';
程式會先執行a[0]裡面的所有
才會執行a[1]裡面的所
最後在執行a[2]裡的所
所以就算同一階層
程式要執行完a[1]裡面的所有東西
才會往a[2]邁進
執行順序一律看第一個門
$a[0] = 11;
$a[1] = array(111,222,333);
$a[2] = 33;
foreach($a as $k => $v){
echo $k . ':';
if (gettype($v) == 'array'){
foreach($v as $kk => $vv){
echo $vv . ' ';
}
echo '<br />';
}else{
echo $v . '<br />';
}
}
do...while....至少做一次,再判斷
實例:至少做一次抽牌再判斷有沒有重複
$poker = array();
for ($i = 0; $i < 52; $i++){
do {
$temp = rand(0,51);
// 檢查機制
$isRepeat = false;
for ($j=0; $j<$i; $j++){
if ($poker[$j] == $temp){
// 哇, 重複了
$isRepeat = true;
break;
}
}
}while($isRepeat);
$poker[] = $temp;
echo $temp . '<br />';
}
echo '---';
while($isRepeat); //$isRepeat這個有重複 有,進入印出
$poker[] = $temp;
echo $temp . '<br />';
}
https://www.php.net/manual/en/function.array
https://www.php.net/manual/en/function.range.php
$poker = range(0, 51); //照順序的撲克牌1~52
shuffle($poker); //打亂
//只印出值(沒有=>)
foreach ($poker as $v) {
echo $v . '</tr>';
}
echo "----<br>"
有=>(的)才會有key與value,否則只有value
ex:
foreach ($players as $player) { //只取value
sort($player);
echo '<tr>';
foreach ($player as $card) {
echo "<td>";
echo $suits[(int)($card / 13)];
echo $values[$card % 13];
echo "</td>";
}
echo '</tr>';
}
$players = [[], [], [], []]; //二維 發牌
foreach ($poker as $i => $card) {
$players[1?][2?] = $card;
}
$players = [[], [], [], []]; //二維 發牌
foreach ($poker as $i => $card) {
$players[$i % 4][(int)($i / 4)] = $card;
}
4*13的表格
<tr>
<td>123132</td>
<td>123132</td>
<td>123132</td>
<td>123132</td>
</tr>
<tr>
<?php
foreach ($players[0] as $card) {
echo "<td>{$card}</td>";
}
?>
<tr>
<?php
echo '<tr>';
foreach ($players[0] as $card) {
echo "<td>{$card}</td>";
}
echo '<tr>';
?>
<?php
//每個玩家($players)內的每一玩家($player)(players's key)內都要做一次
foreach ($players as $player) {
echo '<tr>';
//$players[0]改成$player:讀取每個玩家手上的牌
foreach ($player as $card) {
echo "<td>{$card}</td>";
}
echo '<tr>';
}
?>
♠<br>
♥<br>
♦<br>
♣<br>
$suits = [
"♠",
"<font color='red'>♥</font>",
"<font color='red'>♦</font>",
"♣"
];
$values = ['A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K'];
foreach ($players as $player) {
echo '<tr>';
//player:讀取每個玩家手上的牌
//計算出規則(♠♥♦♣%13各0.1.2.3.4....拿到key)
foreach ($player as $card) {
echo "<td>";
//除以13取整數(花色)
echo $suits[(int)($card / 13)];
//除以13取餘數(牌面)
echo $values[$card % 13];
echo "</td>";
}
echo '</tr>';
}
PHP官方
https://www.php.net/manual/en/function.sort
foreach ($players as $player) {
sort($player);
echo '<tr>';
//player:讀取每個玩家手上的牌
//計算出規則(♠♥♦♣%13各0.1.2.3.4....拿到key)
foreach ($player as $card) {
echo "<td>";
//除以13取整數(花色)
echo $suits[(int)($card / 13)];
//除以13取餘數(牌面)
echo $values[$card % 13];
echo "</td>";
}
echo '</tr>';
}
?>