jeudi 26 juillet 2018

Dynamicaly created array in if statement inside foreach loop

I'm trying to solve, what seems an easy challenge, in PHP language but have run into a wall. Task is to assemble sort of phone book from .txt file (I called mine stdin.txt). Given n names and phone numbers, assemble a phone book that maps friends' names to their respective phone numbers. You will then be given an unknown number of names to query your phone book for. For each name queried, print the associated entry from your phone book on a new line in the form name=phoneNumber; if an entry for name is not found, print Not found instead.

The first line contains an integer, n, denoting the number of entries in the phone book. Each of the subsequent lines describes an entry in the form of 2 space-separated values on a single line. The first value is a friend's name, and the second value is an 8-digit phone number.

After the n lines of phone book entries, there are an unknown number of lines of queries. Each line (query) contains a name to look up, and you must continue reading lines until there is no more input.

My problem is that dynamicaly created array does not return true inside foreach loop, in if statement, but hardcoded array returns true. I have pasted my code, and problems are commented. I feel there is something I'm missing, some simple thing.

stdin.exe file looks like this: 3 sam 99912222 tom 11122222 harry 12299933 sam edward harry

Output should be: sam=99912222 Not found harry=12299933

My code:

$stdin = fopen("stdin.txt", "r");
$dictionarie = stream_get_contents($stdin);
$dict_array = explode("\n", $dictionarie);

$entriesNum = $dict_array[0];
$elemNum = count($dict_array);
$pBook = [];
$queries = [];
//$pBook = ["harry" => 12299933, "tom" => 11122222, "sam" => 99912222];
//$queries = ["harry", "sam", "edward"];

for($i = 1; $i <= $entriesNum; $i++)
{
    $entry = explode(" ", $dict_array[$i]);
    $pBook[$entry[0]] = $entry[1];
}

for($i = $entriesNum + 1; $i < $elemNum; $i++)
{
    array_push($queries, $dict_array[$i]);
}

//printing and testing $pBook array
echo "printing and testing \$pBook array\n";
print_r($pBook);
var_dump(array_key_exists("sam", $pBook))."\n";
if(array_key_exists("harry", $pBook))
{
    echo "exists\n\n";
} else
{
    echo "does not exist\n\n";
}

//printing and testing $queries array
echo "printing and testing \$queries array\n";
/*
 * whent testing in_array() function with dynamicaly created array 
 * only last entry will be found, all other elements won't be found, but
 * when using array with hardcoded values (see commented arrays $pBook & $queries), then
 * all elements are found
*/
print_r($queries);
if(in_array("edward", $queries))
{
    echo "exists\n\n";
} else
{
    echo "does not exist\n\n";
}

echo "===============================\n";
echo "foreach loop testing\n\n";
foreach($queries as $i => $query)
{
    // this echo is for control
    echo "=>".$query."\n";

    /*
     * outside of this foreach loop, array_key_exists() function
     * finds all associative keys, but in foreach loop doesn't find them, but
     * if $pBook array is hardcoded (see commented arrays), then associtative keys are found.
     * 
     * for testing purposes I have var dumped $pBook array if assoc key is not found, and all
     * elements are in array.
    */
    if(array_key_exists($query, $pBook))
    {
        echo $query."=".$pBook[$query]."\n\n";
    } else
    {
        var_dump($pBook);
        echo "Not found\n\n";
    }
}

echo "\n===============================\n";

fclose($stdin);

Aucun commentaire:

Enregistrer un commentaire