【问题标题】:Why won't PHP recognize two equal strings?为什么 PHP 不能识别两个相等的字符串?
【发布时间】:2013-09-12 13:09:34
【问题描述】:

我正在开发一个比较两个数组的组件的 php 函数。数组中的每个值只有一个英文单词长。没有空间。没有字符。

数组#1:英语中最常用单词的列表 语言。 $common_words_array

数组#2:用户生成的句子,转换为小写,剥离 标点符号,exploded() 使用空格 (" ") 作为分隔符。 $study_array

还有一个 $com_study 数组,在本例中用于保存 跟踪被替换的常用词的顺序 $study_array 由一个“_”字符组成。

使用嵌套的 for 循环,脚本应该将 Array #2 中的每个值与 Array #1 中的每个值进行比较。当它找到一个匹配项(也就是一个常用的英语单词)时,它会做一些与当前问题无关的其他魔法。

截至目前,PHP 无法识别两个数组字符串值何时相等。我在这里将代码添加到有问题的函数中以供参考。我添加了许多不必要的回显命令,以便将问题本地化到 if 语句。

谁能看到我错过的东西?同样的算法在 Python 中也能完美运行。

function create_question($study_array, $com_study, $common_words_array)
{
for ($study=0; $study<count($study_array); $study++)
    {
    echo count($study_array)." total in study_array<br>";
    echo "study is ".$study."<br>";

    for ($common=0; $common<count($common_words_array); $common++)
        {
        echo count($common_words_array)." total in common_words_array<br>";
        echo "common is ".$common."<br>";

        echo "-----<br>";
        echo $study_array[$study]." is the study list word<br>";
        echo $common_words_array[$common]." is the common word<br>";
        echo "-----<br>";

          // The issue happens right here.
          if ($study_array[$study] == $common_words_array[$common])
            {
            array_push($com_study, $study_array[$study]);
            $study_array[$study] = "_";
            print_r($com_study);
            print_r($study_array);
            }
        }
    }

    $create_question_return_array = array();
    $create_question_return_array[0] = $study_array;
    $create_question_return_array[1] = $com_study;

    return $create_question_return_array;
}

编辑:在你们这些了不起的程序员的建议下,我更新了 if 语句,使其更加简单,便于调试。见下文。仍然存在未激活 if 语句的相同问题。

if (strcmp($study_array[$study],$common_words_array[$common])==0)
{
echo "match found";
//array_push($com_study, $study_array[$study]);
//$study_array[$study] = "_";
//print_r($com_study);
//print_r($study_array);
}

编辑:应 bansi 的要求,这是我调用函数的主界面 sn-p。

$testarray = array();

$string = "This is a string";
$testarray = create_study_string_array($string);

$testarray = create_question($testarray, $matching, $common_words_array);

至于结果,我只是得到一个空白屏幕。我希望简化的 echo 语句将“找到匹配”输出到屏幕上,但这并没有发生。

【问题讨论】:

  • $study_array[study] 应该是 $study_array[$study]
  • 嗯,这很尴尬。感谢那。但即使有了这个修复,我似乎也无法得到 print_r() $com_study 和 $study_array 的代码。有什么想法吗?
  • 你能告诉我们输出以及你是如何调用函数的(我是指调用变量)吗?
  • 当然可以。给我一点时间再次编辑问题。

标签: php arrays comparison-operators


【解决方案1】:

(编辑)确保您的拆分函数删除了多余的空格(例如preg_split("\\s+", $input))并且输入正确规范化(小写字母、特殊字符等)。

【讨论】:

  • 简单地用回车替换
    标签就可以让 if 语句为您工作?
  • @elersong 对不起,不,我不是那个意思——我只是把它们变成回车,因为我是从控制台运行的,而不是浏览器。我没有对您的代码进行任何其他更改,它检测到“a”和“bear”是常用词,这就是为什么查看一组不适合您的输入会有所帮助的原因。
  • 我更新了 $common_words_array 以保存值('this'、'is'、'a'),仅此而已。
  • 但是,我更进一步,将 if 语句替换为:
  • if (strcmp($common_words_array[$common],"the")==0)
【解决方案2】:

在移动设备上,似乎无法复制文本。在 push 命令中访问研究数组时忘记了美元符号。

【讨论】:

    【解决方案3】:

    改变

    array_push($com_study, $study_array[study]);
    

    array_push($com_study, $study_array[$study]);
     // You missed a $                  ^ here
    

    编辑:
    以下代码输出 3 'match found'。我不知道$common_words_array$matching 的值,所以我使用了一些任意值,也没有使用函数create_study_string_array,我只是使用了explode。仍然很困惑,无法弄清楚您到底想要实现什么。

    <?php
    $testarray = array ();
    
    $string = "this is a string";
    $testarray = explode ( ' ', $string );
    $common_words_array = array (
            'is',
            'a',
            'this' 
    );
    $matching = array (
            'a',
            'and',
            'this' 
    );
    
    $testarray = create_question ( $testarray, $matching, $common_words_array );
    
    function create_question($study_array, $com_study, $common_words_array) {
        echo count ( $study_array ) . " total in study_array<br>";
        echo count ( $common_words_array ) . " total in common_words_array<br>";
        for($study = 0; $study < count ( $study_array ); $study ++) {
            // echo "study is " . $study . "<br>";
    
            for($common = 0; $common < count ( $common_words_array ); $common ++) {
                // The issue happens right here.
                if (strcmp ( $study_array [$study], $common_words_array [$common] ) == 0) {
                    echo "match found";
                }
            }
        }
    
        $create_question_return_array = array ();
        $create_question_return_array [0] = $study_array;
        $create_question_return_array [1] = $com_study;
    
        return $create_question_return_array;
    }
    
    ?>
    

    输出:

    4 total in study_array
    3 total in common_words_array
    match foundmatch foundmatch found
    

    【讨论】:

      【解决方案4】:

      使用 === 而不是 ==

      if ($study_array[$study] === $common_words_array[$common])
      

      或者更好地使用strcmp

       if (strcmp($study_array[$study],$common_words_array[$common])==0)
      

      【讨论】:

      • 我现在正在尝试,但从语义上讲,为什么它比 if 语句更好?
      • == 检查是否相等,=== 检查值是否相等且变量类型是否相同。
      • 我进行了更改以包含 strcmp() 函数,但即便如此,我还是没有运气。我真的希望 jsfiddle 有一个 PHP 选项。
      【解决方案5】:

      尽可能使用内置函数以避免不必要的代码和拼写错误。此外,提供示例输入也会有所帮助。

      $study_array = array("a", "cat", "sat", "on","the","mat");
      $common_words_array = array('the','a');
      $matching_words = array();
      
      foreach($study_array as $study_word_index=>$study_word){
      
          if(in_array($study_word, $common_words_array)){
      
              $matching_words[] = $study_word;
      
              $study_array[$study_word_index] = "_";
      
              //do something with matching words
          }
      
      }
      
      print_r($study_array);
      
      print_r($matching_words);
      

      【讨论】:

      • 我也试过了,但是应该用“_”替换常用词的行不起作用。当我 print_r() $study_array 时,它看起来与运行该函数之前的操作相同。知道为什么吗?
      • 我已将 $common_words_array 更改为包含 ('this', 'is', 'a') ,仅此而已。仍然无法识别匹配项。
      猜你喜欢
      • 1970-01-01
      • 2016-03-19
      • 1970-01-01
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      相关资源
      最近更新 更多