Php 函数 in_array() 不返回预期结果

我正在尝试获取从数据库表中获取的数组中是否存在名称。该数组包含表的全名字段members 但是,在使用in_array()函数时,任何搜索的结果始终为假


这是数组的样子


    Array ( [0] => Array ( [name] => Jane ) 

            [1] => Array ( [name] => Kelvin ) 

            [2] => Array ( [name] => James ) 

            [3] => Array ( [name] => Leon ) 

            [4] => Array ( [name] => Steve ) 

            [5] => Array ( [name] => Jenifer ) 

           ) 


这是我的php 代码,用于检查名称是否存在


<?php


    require_once 'databaseconn.php'; 

    #To get connection

    $mysqli = new mysqli($db_hostname, $db_username, $db_password,$db_database);

    #To get array of names from my database

    $stmt = $mysqli-> prepare('SELECT name FROM members WHERE id');

    $stmt->execute();

    $result = $stmt->get_result();

    #To store result as array in variable $application_names

    while ($row = $result->fetch_assoc()) {

        $member_names[] = $row;

    }

    #To test if name exists

    $name = "Jane";


    if (in_array($name, $member_names))

    {

    echo "Match found";

    }

    else

    {

    echo "Match not found";

    }


?>


慕勒3428872
浏览 164回答 1
1回答

手掌心

<?php&nbsp; &nbsp; require_once 'databaseconn.php';&nbsp;&nbsp; &nbsp; #To get connection&nbsp; &nbsp; $mysqli = new mysqli($db_hostname, $db_username, $db_password,$db_database);&nbsp; &nbsp; #To get array of names from my database&nbsp; &nbsp; $stmt = $mysqli-> prepare('SELECT name FROM members WHERE id');&nbsp; &nbsp; $stmt->execute();&nbsp; &nbsp; $result = $stmt->get_result();&nbsp; &nbsp; #To store result as array in variable $application_names&nbsp; &nbsp; while ($row = $result->fetch_assoc()) {&nbsp; &nbsp; &nbsp; &nbsp; $application_names[] = $row;&nbsp; &nbsp; }&nbsp; &nbsp; #To test if name exists&nbsp; &nbsp; $name = "Jane";&nbsp; &nbsp; $flatArr = [];&nbsp; &nbsp; foreach ($member_names as $memberArr) {&nbsp; &nbsp; &nbsp; &nbsp; if(isset($memberArr['name'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $flatArr[] = $memberArr['name'];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (in_array($name,&nbsp; $flatArr))&nbsp; &nbsp; {&nbsp; &nbsp; echo "Match found for: " . $name;&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; echo "Match not found for: " . $name;&nbsp; &nbsp; }?>
打开App,查看更多内容
随时随地看视频慕课网APP