我一直试图让这个" mastermind-game"现在工作了几个小时,但即使array_intersect
也不能正常工作,我只会收到第一个数组作为新阵列。
我的两个目标是:
代码:
<?php
$counter = $_POST['counter'];
$eins = $_POST['eins'];
$zwei = $_POST['zwei'];
$drei = $_POST['drei'];
$vier = $_POST['vier'];
$fuenf = $_POST['fuenf'];
if($counter > 0){
$elf = $_POST['elf'];
$zwoelf = $_POST['zwoelf'];
$dreizehn = $_POST['dreizehn'];
$vierzehn = $_POST['vierzehn'];
$fuenfzehn = $_POST['fuenfzehn'];
}
else{
$elf = 0;
$zwoelf = 0;
$dreizehn = 0;
$vierzehn = 0;
$fuenfzehn = 0;
}
$inputeins[0] = $eins;
$inputeins[1] = $zwei;
$inputeins[2] = $drei;
$inputeins[3] = $vier;
$inputeins[4] = $fuenf;
$inputdrei[0] = $elf;
$inputdrei[1] = $zwoelf;
$inputdrei[2] = $dreizehn;
$inputdrei[3] = $vierzehn;
$inputdrei[4] = $fuenfzehn;
if($inputeins == $inputdrei){
echo "<br><a href='ratespiel.html' style='font-size:100px;'>Gewonnen!</a>";
}
else{
if($counter < 12){
echo "<form action='ratespielzwei.php' method='post'>";
echo "Ziffern von Spieler 2:<br><br>";
echo "<input type='number' name='counter' value='$counter' hidden>";
echo "<input type='number' name='eins' value='$eins' hidden>";
echo "<input type='number' name='zwei' value='$zwei' hidden>";
echo "<input type='number' name='drei' value='$drei' hidden>";
echo "<input type='number' name='vier' value='$vier' hidden>";
echo "<input type='number' name='fuenf' value='$fuenf' hidden>";
echo "<input type='number' min='1' max='5' name='sechs' style='width:30px;' required> ";
echo "<input type='number' min='1' max='5' name='sieben' style='width:30px;' required> ";
echo "<input type='number' min='1' max='5' name='acht' style='width:30px;' required> ";
echo "<input type='number' min='1' max='5' name='neun' style='width:30px;' required> ";
echo "<input type='number' min='1' max='5' name='zehn' style='width:30px;' required> ";
echo "<input type='submit' min='1' max='5' name='submitzwei' value='OK'><br><br>";
echo "</form>";
}
else{
echo "<br><a href='ratespiel.html' style='font-size:100px;'>Verloren!</a>";
}
}
if($counter > 0){
foreach ($inputdrei as $ausgabe){
echo "$ausgabe ";
}
}
?>
和
<?php
$counter = $_POST['counter'];
$i = $counter + 1;
$eins = $_POST['eins'];
$zwei = $_POST['zwei'];
$drei = $_POST['drei'];
$vier = $_POST['vier'];
$fuenf = $_POST['fuenf'];
$sechs = $_POST['sechs'];
$sieben = $_POST['sieben'];
$acht = $_POST['acht'];
$neun = $_POST['neun'];
$zehn = $_POST['zehn'];
$inputeins[0] = $eins;
$inputeins[1] = $zwei;
$inputeins[2] = $drei;
$inputeins[3] = $vier;
$inputeins[4] = $fuenf;
$inputzwei[0] = $sechs;
$inputzwei[1] = $sieben;
$inputzwei[2] = $acht;
$inputzwei[3] = $neun;
$inputzwei[4] = $zehn;
while($counter <= $i){
$counter++;
}
if($inputeins == $inputzwei){
echo "<br><a href='ratespiel.html' style='font-size:100px;'>Gewonnen!</a>";
}
else{
echo "<form action='ratespieleins.php' method='post'>";
echo "Ziffern von Spieler 2:<br><br>";
echo "<input type='number' name='counter' value='$counter' hidden>";
echo "<input type='number' name='eins' value='$eins' hidden>";
echo "<input type='number' name='zwei' value='$zwei' hidden>";
echo "<input type='number' name='drei' value='$drei' hidden>";
echo "<input type='number' name='vier' value='$vier' hidden>";
echo "<input type='number' name='fuenf' value='$fuenf' hidden>";
echo "<input type='number' min='1' max='5' name='elf' style='width:30px;' required> ";
echo "<input type='number' min='1' max='5' name='zwoelf' style='width:30px;' required> ";
echo "<input type='number' min='1' max='5' name='dreizehn' style='width:30px;' required> ";
echo "<input type='number' min='1' max='5' name='vierzehn' style='width:30px;' required> ";
echo "<input type='number' min='1' max='5' name='fuenfzehn' style='width:30px;' required> ";
echo "<input type='submit' name='submitdrei' value='OK'><br><br>";
echo "</form>";
}
foreach ($inputzwei as $ausgabe){
echo "$ausgabe ";
}
?>
答案 0 :(得分:1)
如果要比较值和键(索引),可以使用array_intersect_assoc()。
答案 1 :(得分:0)
我能够通过array_intersect_assoc
实现我的目标:
$intersectnumbers = array_intersect($inputeins, $inputdrei);
$intersectunique = array_unique($intersectnumbers);
echo "<p>Anzahl richtiger Ziffern: " . count($intersectunique) . "</p>";
$intersectposition = array_intersect_assoc($inputeins, $inputdrei);
echo "<p>Anzahl richtiger Positionen: " . count($intersectposition) . "</p>";