在 foreach 循环内的 if 语句中动态创建数组

Dynamicaly created array in if statement inside foreach loop

我正在尝试用 PHP 语言解决看似简单的挑战,但遇到了 运行 问题。任务是从 .txt 文件中 assemble 排序 phone 本书(我叫我的 stdin.txt)。给定 n 个名字和 phone 个号码,assemble 一本 phone 将朋友的名字映射到他们各自的 phone 号码的书。然后,您将获得未知数量的姓名来查询您的 phone 书籍。对于每个 name 查询,将 phone 书中的相关条目以 name=phoneNumber 形式打印在新行上;如果找不到 name 的条目,则打印 Not found

第一行包含一个整数,n,表示phone本书中的条目数。 随后的每一行都在一行中以 2 space 分隔值的形式描述了一个条目。第一个值是朋友的名字,第二个值是一个 8 位数字 phone。

n行phone本书条目后,还有未知行数的查询。每行(查询)包含一个 name 来查找,您必须继续阅读行,直到没有更多输入。

我的问题是,在 if statement 中,动态创建的数组在 foreach 循环中不 return 为真,但硬编码数组 return 为真。我已经粘贴了我的代码,并评论了问题。我觉得我缺少一些东西,一些简单的东西。

stdin.exe 文件如下所示: 3 sam 99912222 tom 11122222 harry 12299933 sam edward harry

输出应该是: sam=99912222 Not found harry=12299933

我的代码:

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

我必须 trim 输入才能让它工作。

当然还有遍历 $queries 数组来查找人数。

<?php
$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 = [];

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

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

//printing and testing $pBook array
echo "printing and testing $pBook array\n";
var_dump($pBook);
var_dump($queries);

foreach ($queries as $who) {
    echo 'looking for ' .$who.PHP_EOL;
    if ( array_key_exists($who, $pBook) ) {
        echo $pBook[$who] . PHP_EOL;
    } else {  
        echo 'Not Found'.PHP_EOL;
    }
}