我试图获取从数据库表中获取的数组中是否存在名称。该数组包含表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";
}
?>
答案 0 :(得分:0)
<?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;
}
?>