Php 被数组和爆炸空间卡住了

Php 被数组和爆炸空间卡住了,php,mysql,arrays,explode,Php,Mysql,Arrays,Explode,我有一个名为mystique item的应用程序,用户必须猜测水印后面的项目。水位线不时地显现出来,一切都很完美,但我在猜单词方面有一个“小”问题。我在数组中添加了单词,用逗号分隔,我在php中扩展了这个数组,但是出于某种原因,它只捕获第一个单词作为正确的,其他所有的都是错误的。以下是我所做的 $gt = getVal('pics','gtext','online',1); $won = getVal('pics','winner','online',1); if($won=='no') {

我有一个名为mystique item的应用程序,用户必须猜测水印后面的项目。水位线不时地显现出来,一切都很完美,但我在猜单词方面有一个“小”问题。我在数组中添加了单词,用逗号分隔,我在php中扩展了这个数组,但是出于某种原因,它只捕获第一个单词作为正确的,其他所有的都是错误的。以下是我所做的

$gt = getVal('pics','gtext','online',1);
$won = getVal('pics','winner','online',1);

if($won=='no')
{
    $counts = getGen(3);
    $counts2 = getGen(4);

    if($counts2==0) 
    {
         $counts2 = 9999999999999;
    }

    $ccount =  getCount2("$uid","$pid",date('Y-m-d H:i:s',$t1),date('Y-m-d H:i:s',$t2));
    $ccount3 =  getCount3("$uid","$pid");   

    if( $ccount>=$counts || $ccount3>=$counts2)
    {
         echo '4';
    }
    else
    {
        $sp = explode(",",$gt);

        if(in_array($val, $sp)) // guess correct
        { 
            echo '1';
        }
        else// guess wrong
        {
            echo '2';
        }
     }
}
gtext
是我存储单词的行,单词中有空格,例如:
new iphone、iphone 5s、apple ipad等)

下面是检查单词的代码:

$.post('guessit.php',{from:1,val:$('#ug').val(),uid:$('#uid').val(),pid:$('#pid').val(),t1:<?php echo $time1; ?>,t2:<?php echo $time3; ?>},function(d){
  if(parseInt(d)==1){
    $.post('guessit.php',{from:2,val:$('#ug').val(),uid:$('#uid').val(),pid:$('#pid').val(),t1:<?php echo $time1; ?>,t2:<?php echo $time3; ?>},function(d1){
      advanced_example($('#uid').val(),'Congratulations!','You are the winner!!',1);
      //setInterval($(location).attr('href','redirecttohome.php'),10000);
    });
  }else if(parseInt(d)==2){
    $.post('guessit.php',{from:3,val:$('#ug').val(),uid:$('#uid').val(),pid:$('#pid').val(),t1:<?php echo $time1; ?>,t2:<?php echo $time3; ?>},function(d1){
      advanced_example($('#uid').val(),'Wrong!','Please try again!',1);              
      //setInterval($(location).attr('href','redirecttohome.php'),10000);
    });
  }else if(parseInt(d)==3){
    advanced_example($('#uid').val(),'Sorry!','Someone else was faster!',1);
    //setInterval($(location).attr('href','redirecttohome.php'),8000);
  }else if(parseInt(d)==4){
    advanced_example($('#uid').val(),'Error!','You already attempted maximum times',1);             
    //setInterval($(location).attr('href','redirecttohome.php'),8000);

}
我需要它们作为字符串:

apple ipad
apple iphone4
apple ipod
iphone4
apple

您需要修剪
if
条件的空白,以使其按您的需要工作:

$sp = explode(",",$gt);
$sp = array_map('trim', $sp); //trim all the elements in $sp
如果元素的开头或结尾包含空格,则以下条件将计算为
FALSE
,从而触发
else
块中的语句:

if(in_array($val, $sp)) {

如果删除了空格,代码应该可以正常工作。

array\u walk($sp,'trim')好的,我会在我自己的代码中尝试,会让你们知道的。基本上,这将识别短语中的空格,但是为什么第一个带空格的单词会起作用呢?还有什么东西没有?我只是想澄清一下。Thanks@JognSmit:可能是因为字符串中有多余的空格。我的短语只在单词之间有空格,不在单词的开头/结尾。现在我想我明白你在说什么了,我会马上测试:)噩梦我永远不会想到这个主意。谢谢。@JognSmit:是的,你试过使用我在回答中提到的
array\u map
if(in_array($val, $sp)) {