今天我在弄清楚PHP中的一些事情时遇到了一点麻烦。因为我通常会有点跑题的倾向,所以我会尽量让我的问题简短而简洁。这就是我要做的。
array# 1是一个多维数组,包含对数据库中的用户表执行的MySQL查询的结果。它看起来像这样:
Array ( [0] => Array ( [id] => 79 [firstname] => John [lastname] => Doe [province] => Province [email] => someemail@gmail.com [primaryphone] => 123-456-7890 ) [1] => Array ( [id] => 113 [firstname] => Jane [lastname] => Doe [province] => Province [email] => email@gmail.com [primaryphone] => 123-456-7890 ) )
array# 2是另一个多维数组,其中包含对数据库中的成员表进行MySQL查询的结果,该成员表包含与当前组相关联的用户的id。它看起来如下:
Array ( [0] => Array ( [userid] => 79 ) [1] => Array ( [userid] => 115 ) [2] => Array ( [userid] => 124 ) )
我要做的是,对于数组#1中返回的每个用户,在数组#2中查找与数组#1中的值[id]
匹配的[userid]
下的值。如果为每个用户找到匹配,则将该用户的键和值[ismember] => 1
附加到数组#1中—如果没有匹配,则将对[ismember] => 0
附加到数组#1中。
我觉得这应该是一个非常简单的过程,我可能只是错过了一些基本的东西…但我已经做了一段时间了,并没有取得多大进展。谢谢大家的时间和帮助。
== EDIT ==第一个数组是由以下MySQL查询生成的:
$query = "
SELECT
id,
firstname,
lastname,
province,
email,
primaryphone
FROM userstable
";
try
{
$stmt = $db->prepare($query);
$stmt->execute();
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
$allusersdetails = $stmt->fetchAll();
第二个数组由查询生成:
$query = "
SELECT
userid
FROM membershipstable
WHERE templateid = :currenttemplateid
";
try
{
$stmt = $db->prepare($query);
$stmt->bindParam(':currenttemplateid', $currenttemplateid);
$stmt->execute();
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
$templateusers = $stmt->fetchAll();
您可以将两个查询替换为一个查询,如下所示:
$query = "
SELECT
u.id,
u.firstname,
u.lastname,
u.province,
u.email,
u.primaryphone,
(CASE WHEN m.userid IS NULL THEN 0 ELSE 1 END) AS ismember
FROM userstable AS u
LEFT JOIN membershipstable AS m ON
m.templateid = :currenttemplateid AND
u.id = m.userid
";
try
{
$stmt = $db->prepare($query);
$stmt->bindParam(':currenttemplateid', $currenttemplateid);
$stmt->execute();
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
$allusersdetails = $stmt->fetchAll();
这仍然会拉出所有的用户,不管他们是否是成员,但是,它会添加ismember
作为列。
(userstable AS u
和membershipstable AS m
的AS x
部分只是在SELECT
期间分配字母作为表的别名)。基本上,它主要帮助您节省输入,但也允许您在具有相同名称的列的表上使用JOIN
。
欢迎来到结构化查询语言的奇妙世界:D
编辑为了防止有人来做题目要求的事情但不是查询数据库但确实有OP格式的数组
<?php
$array =
Array ( 0 => Array ( 'id' => '79','firstname' => 'John','lastname' => 'Doe','province' => 'Province','email' => 'someemail@gmail.com','primaryphone' => '123-456-7890',), 1 => Array ( 'id' => '113','firstname' => 'Jane','lastname' => 'Doe','province' => 'Province','email' => 'email@gmail.com','primaryphone' => '123-456-7890',) )
;
$array2 =
Array ( 0 => Array ( 'userid' => '79'), 1 => Array ( 'userid' => '115'), 2 => Array ( 'userid' => '124') );
foreach($array as $key => $userInfo) {
$isMember = 0;
foreach($array2 as $user) {
if($userInfo['id'] == $user['userid']) {
$isMember = 1;
break;
}
}
$array[$key]['ismember'] = $isMember;
}
试试这个,
$firstArray = //your first array
$secondArray = // your second array
foreach($firstArray as $fa) {
$id = $fa['id'];
}
foreach($secondArray as $sa) {
$userId = $sa['userid'];
}
if($id === $userID) {
$firstArray['ismember'] = 1;
} else {
$firstArray['ismember'] = 0;
}