Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy won't PHP recognize two equal strings?
    primarykey
    data
    text
    <p>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. </p> <blockquote> <p>Array #1: a list of the most commonly used words in the english language. $common_words_array</p> <p>Array #2: a user-generated sentence, converted to lowercase, stripped of punctuation, and exploded() using the space (" ") as a delimiter. $study_array</p> <p>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.</p> </blockquote> <p>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. </p> <p>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. </p> <p>Can anybody see something that I've missed? The same algorithm worked perfectly in Python.</p> <pre><code>function create_question($study_array, $com_study, $common_words_array) { for ($study=0; $study&lt;count($study_array); $study++) { echo count($study_array)." total in study_array&lt;br&gt;"; echo "study is ".$study."&lt;br&gt;"; for ($common=0; $common&lt;count($common_words_array); $common++) { echo count($common_words_array)." total in common_words_array&lt;br&gt;"; echo "common is ".$common."&lt;br&gt;"; echo "-----&lt;br&gt;"; echo $study_array[$study]." is the study list word&lt;br&gt;"; echo $common_words_array[$common]." is the common word&lt;br&gt;"; echo "-----&lt;br&gt;"; // 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; } </code></pre> <p>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.</p> <pre><code>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); } </code></pre> <p>EDIT: At bansi's request, here's the main interface snippet where I'm calling the function.</p> <pre><code>$testarray = array(); $string = "This is a string"; $testarray = create_study_string_array($string); $testarray = create_question($testarray, $matching, $common_words_array); </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload