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

时间:2018-07-26 23:48:32

标签: php arrays loops if-statement fopen

我正在尝试用PHP语言解决似乎是一个简单的挑战,但是遇到了麻烦。任务是从.txt文件(我称为mine stdin.txt)组装电话簿。给定n的名称和电话号码,组装一本电话簿,将朋友的姓名映射到他们各自的电话号码。然后,将为您提供未知数量的名称,以查询电话簿。对于每个查询的name,将电话簿中的相关条目以name=phoneNumber的形式换行打印;如果找不到name的条目,请打印Not found

第一行包含一个整数n,表示电话簿中的条目数。 随后的每一行在一行上以2个以空格分隔的值的形式描述一个条目。第一个值是朋友的名字,第二个值是8位数字的电话号码。

在n行电话簿条目之后,存在未知数量的查询行。每行(查询)都包含一个name来查找,并且您必须继续读取行,直到没有更多输入为止。

我的问题是,动态创建的数组在if statement中的foreach循环内没有返回true,但是硬编码的数组返回了true。我已经粘贴了我的代码,并对问题进行了评论。我觉得我缺少一些简单的东西。

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

1 个答案:

答案 0 :(得分:2)

我必须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;
    }
}
相关问题