我有两个数组,其中我混合了键和值,直到其中一个数组为空。但我的循环执行无穷无尽。如果arrayOne中的键大于arrayTwo。值从arrayTwo中删除并添加到arrayOne。这是我的代码
$cardsOfPlayerOne = array(
"karo" => array(
'1' => 'dwa',
'2' => 'trzy'
),
"kier" => array(
'3' => 'dwa',
'4' => 'trzy'
));
$cardsOfPlayerTwo = array(
"trefl" => array(
'5' => 'dwa',
'6' => 'trzy'
),
"pik" => array(
'7' => 'dwa',
'8' => 'trzy'
));
while (!empty($cardsOfPlayerOne) OR !empty($cardsOfPlayerTwo)){
$cardKeyOne = array_rand($cardsOfPlayerOne);
$cardKeyTwo = array_rand($cardsOfPlayerTwo);
$cardStrenghtOne = array_rand($cardsOfPlayerOne[$cardKeyOne]);
$cardStrenghtTwo = array_rand($cardsOfPlayerTwo[$cardKeyTwo]);
if($cardStrenghtOne > $cardStrenghtTwo){
if(!isset($cardsOfPlayerOne[$cardKeyTwo][$cardStrenghtTwo])){
$cardsOfPlayerOne[$cardKeyTwo][$cardStrenghtTwo] = array();
}
$cardsOfPlayerOne[$cardKeyTwo][$cardStrenghtTwo] = $cardsOfPlayerTwo[$cardKeyTwo][$cardStrenghtTwo];
unset($cardsOfPlayerTwo[$cardKeyTwo][$cardStrenghtTwo]);
if(empty($cardsOfPlayerTwo[$cardKeyTwo])){
unset($cardsOfPlayerTwo[$cardKeyTwo]);
}
}
else{
if(!isset($cardsOfPlayerTwo[$cardKeyOne][$cardStrenghtOne])){
$cardsOfPlayerTwo[$cardKeyOne][$cardStrenghtOne] = array();
}
$cardsOfPlayerTwo[$cardKeyOne][$cardStrenghtOne] = $cardsOfPlayerOne[$cardKeyOne][$cardStrenghtOne];
unset($cardsOfPlayerOne[$cardKeyOne][$cardStrenghtOne]);
if(empty($cardsOfPlayerOne[$cardKeyOne])){
unset($cardsOfPlayerOne[$cardKeyOne]);
}
}
}
并在线编码LINK
答案 0 :(得分:2)
我认为你的逻辑可能不正确:
while (!empty($cardsOfPlayerOne) OR !empty($cardsOfPlayerTwo)){
应该是:
while (!empty($cardsOfPlayerOne) AND !empty($cardsOfPlayerTwo)){
目前只要两个数组的 不为空,你的循环就会继续,但就我所知,你想在其中一个 <是>之后立即退出/ em>空。