我正在尝试构建一个小游戏,并试图通过匹配我的$ word数组和$正确的guess数组的计数来显示“祝贺”消息,但它们不匹配。
很显然,我的$ word是一个数组,在第一次输入字母后不被识别为数组,它向我显示-为foreach()提供的无效参数-当我尝试显示它时,我找不到一个解决方案
<?php
session_start();
if(empty($_POST['guess'])){
$_SESSION['cg']= array();
$_SESSION['g'] = array();
$_SESSION['wg']= array();
$_SESSION['count']=10;
}
$word = array('a','l','i','g','a','t','o','r');
$alert=false;
if(!empty($_POST['guess'])){
if(!in_array($_POST['guess'],$_SESSION['g'])){
array_push($_SESSION['g'],$_POST['guess']);
$_SESSION['count']--;
if(in_array($_POST['guess'],$word)){
array_push($_SESSION['cg'],$_POST['guess']);
}array_push($_SESSION['wg'],$_POST['guess']);
}else $alert=true; //JS
}
foreach($word as $w){
if(in_array($w,$_SESSION['g'])){
echo $w . " ";
}
else {
echo "_ ";
}
}
if(count($word)==count($_SESSION['cg'])){
echo "CONGRATULATIONS"; //JS
}
if(is_array($word)){
echo "yes";}else echo "no";
?>