Why won't PHP recognize two equal strings?
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;
}
No comments:
Post a Comment