2017-04-11 105 views
1

我一直试图让这个“策划游戏”现在小时的工作,但即使array_intersect将无法​​正常工作,我就收到了第一个数组作为新数组。比较两个数组的相同值(和位置)?

我的两个目标是:

  1. 正确位置的金额
  2. 金额正确的数字

代码:

<?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 "; 
     } 
    ?> 
+1

你能不能给一个最小的工作示例,这样我们就可以看到你的问题,不研究整个代码? https://stackoverflow.com/help/mcve – user4035

+0

I'm仍然对于新手,但我添加了一个例子。说实话,我不知道如何实现我的两个目标。 – blasthrop

+0

请不要发布问题内部解决方案。为此创建自己的答案。 – k0pernikus

回答

1

如果要比较的数值以及钥匙(指数),可以使用array_intersect_assoc()

+0

好吧,这似乎工作,现在我必须找出如何正确使用这些信息。谢谢! – blasthrop

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>";