dongqishun6409 2013-09-09 04:38
浏览 44
已采纳

为什么PHP不能识别两个相等的字符串?

I'm working on a php function that will compare the components of two arrays. Each value in the arrays are only one english word long. No spaces. No characters.

Array #1: a list of the most commonly used words in the english language. $common_words_array

Array #2: a user-generated sentence, converted to lowercase, stripped of punctuation, and exploded() using the space (" ") as a delimiter. $study_array

There's also a $com_study array, which is used in this case to keep track of the order of commonly used words which get replaced in the $study_array by a "_" character.

Using nested for loops, what SHOULD happen is that the script should compare each value in Array #2 to each value in Array #1. When it finds a match (aka. a commonly used english word), it will do some other magic that's irrelevant to the current problem.

As of right now, PHP doesn't recognize when two array string values are equivalent. I'm adding in the code to the problematic function here for reference. I've added in a lot of unnecessary echo commands in order to localize the problem to the if statement.

Can anybody see something that I've missed? The same algorithm worked perfectly in 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;
}

EDIT: At the suggestion of you amazing coders, I've updated the if statement to be much more simple for purposes of debugging. See below. Still having the same issue of not activating the if statement.

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);
}

EDIT: At bansi's request, here's the main interface snippet where I'm calling the function.

$testarray = array();

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

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

As for the result, I'm just getting a blank screen. I would expect to have the simplified echo statement output "match found" to the screen, but that's not happening.

  • 写回答

5条回答 默认 最新

  • ds0409 2013-09-09 04:51
    关注

    (EDIT) make sure your that your splitting function removes excess whitespace (e.g. preg_split("\\s+", $input)) and that the input is normalized properly (lowercase'd, special chars stripped out, etc.).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题