为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕勒3428872 2021-06-27 17:55:21
我正在尝试获取从数据库表中获取的数组中是否存在名称。该数组包含表的全名字段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";    }?>
查看完整描述

1 回答

?
手掌心

TA贡献1942条经验 获得超3个赞

<?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()) {

        $application_names[] = $row;

    }

    #To test if name exists

    $name = "Jane";


    $flatArr = [];


    foreach ($member_names as $memberArr) {

        if(isset($memberArr['name'])) {

            $flatArr[] = $memberArr['name'];

        }

    }


    if (in_array($name,  $flatArr))

    {

    echo "Match found for: " . $name;

    }

    else

    {

    echo "Match not found for: " . $name;

    }


?>


查看完整回答
反对 回复 2021-07-02
  • 1 回答
  • 0 关注
  • 163 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信