Pregunta

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.

¿Fue útil?

Solución

(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.).

Otros consejos

On mobile and can't seem to copy text. You forgot a dollar sign when accessing the study array in your push command.

change

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

to

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

Edit:
The following code outputs 3 'match found'. i don't know the values of $common_words_array and $matching, so i used some arbitrary values, also instead of using function create_study_string_array i just used explode. still confused, can't figure out what exactly you are trying to achieve.

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

?>

Output:

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

Use === instead of ==

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

OR even better use strcmp

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

Use built-in functions wherever possible to avoid unnecessary code and typos. Also, providing sample inputs would be helpful too.

$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);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top