PHP忽略最后一个数组元素

时间:2015-04-01 14:46:08

标签: php arrays

我正在玩一个基本的PHP网站,仅仅是为了我自己的知识,我基本上有一个文本文件填充“密码”。我已将此文本文件作为数组打开到我的页面中,但是当我搜索特定元素时,唯一显示为有效的密码是最后一个选项,它似乎忽略了数组中的每个其他元素。

所以,如果我的密码列表是

  

密码

     

12345

     

测试

     

QWERTY

当我搜索前3个元素时,它表示它在数组中不存在。但是,如果我在这里搜索了最后一个值'qwerty',它将匹配。

<?php
$file = explode("\n", file_get_contents("passwords.txt"));


//Call the function and pass in the list
if (isset($_POST['submit_login'])) {

    //Set the search variable
    $search = $_POST['password'];

    search_array($file, $search);
} else {
    //echo "You did not search! <br />";
    //echo "<a href='searchForm.html'>Try again?</a>";
}

function search_array($array_value, $search_query)
{
    echo "<span><h4>The Array List: </h4><span/>";
    foreach ($array_value as $key => $value) {
        echo $key." ";
        echo $value."<br />";


     }
    if ($value == $search_query) {
        echo "<h5>Search Stuff</h5>";
        echo "You searched for: " . $search_query . "</br>";
        echo "Your search was found in the index #" .$key. "<br />";


    } else {
        echo "You searched for: " . $search_query . "</br>";
        echo "Your search did not match any of the items. <br />";
        echo "<a href='searchForm.html'>Try again?</a>";
    }


}


?>

但是,如果我搜索'12345',例如数组中的索引1,我将得到输出

  

您搜索了:12345

     

您的搜索与任何项目都不匹配。

但是,搜索最后一个元素'qwerty'会产生所需的响应。

3 个答案:

答案 0 :(得分:2)

你的if ($value == $search_query)不在foreach循环中。移动if-else块下面的foreach的右括号,它应该可以工作。

答案 1 :(得分:0)

尝试更改您的&#34; search_array&#34;对此的方法。

function search_array($array_value, $search_query)
{
    echo "<span><h4>The Array List: </h4><span/>";
    $match = false;
    foreach ($array_value as $key => $value) {
        echo $key." ";
        echo $value."<br />";

        if ($value == $search_query) {
           $match = true;
           break;
        }
     }
    if ($match) {
        echo "<h5>Search Stuff</h5>";
        echo "You searched for: " . $search_query . "</br>";
        echo "Your search was found in the index #" .$key. "<br />";
    } else {
        echo "You searched for: " . $search_query . "</br>";
        echo "Your search did not match any of the items. <br />";
        echo "<a href='searchForm.html'>Try again?</a>";
    }
}

密码检查应该在foreach循环中,检查后你可以打印一些东西。

答案 2 :(得分:0)

不要让一切变得如此复杂,只需使用它:

(这里我首先使用file()阅读您的文件。然后只需使用array_search()查看该值是否在数组中并获取密钥)

<?php

    $lines = file("passwords.txt", FILE_IGNORE_NEW_LINES)

    //Call the function and pass in the list
    if (isset($_POST['submit_login'])) {

        //Set the search variable
        $search = $_POST['password'];
        search_array($lines, $search);

    } else {
        //echo "You did not search! <br />";
        //echo "<a href='searchForm.html'>Try again?</a>";
    }

    function search_array($array_value, $search_query) {
        echo "<span><h4>The Array List: </h4><span/>";

        if(($key = array_search($search_query, $array_value)) !== FALSE) {
            echo "<h5>Search Stuff</h5>";
            echo "You searched for: " . $search_query . "</br>";
            echo "Your search was found in the index #" .$key. "<br />";
        } else {
            echo "You searched for: " . $search_query . "</br>";
            echo "Your search did not match any of the items. <br />";
            echo "<a href='searchForm.html'>Try again?</a>";
        }

    }

?>