我正在使用以下代码
# fetch these users.
$statement = $conn->prepare('SELECT * FROM weenkDetails WHERE userId = :userId AND weenkedId IN (' . implode(',', $userIds) . ')');
$statement->bindParam(':userId' , $userId, PDO::PARAM_STR);
$statement->execute();
while ($newRow = $statement->fetch(PDO::FETCH_ASSOC))
{
$weenks[] = $newRow;
}
# assign weenks to users.
# assign users to posts.
foreach ($users as $index => $user)
{
$users[$index]['weenk'] = "null";
foreach ($weenks as $weenk)
{
if ($user['id'] == $weenk['weenkedId'])
{
$users[$index]['weenk'] = $weenk;
break;
}
}
}
$statement = $conn->prepare('SELECT * FROM weenkDetails WHERE weenkedId = :userId AND type = 2 AND userId IN (' . implode(',', $userIds) . ')');
$statement->bindParam(':userId' , $userId, PDO::PARAM_STR);
$statement->execute();
$newWeenks[] = array();
while ($newRow = $statement->fetch(PDO::FETCH_ASSOC))
{
$newWeenks[] = $newRow;
}
# assign weenks to users.
# assign users to posts.
foreach ($users as $index => $user)
{
foreach ($newWeenks as $weenk)
{
if ($user['id'] == $weenk['userId'])
{
$users[$index]['weenk'] = $weenk;
break;
}
}
}
最初作为测试用例,我的$ weenks []和$ newWeenks []是空数组。
foreach ($users as $index => $user)
{
$users[$index]['weenk'] = "null";
foreach ($weenks as $weenk)
{
if ($user['id'] == $weenk['weenkedId'])
{
$users[$index]['weenk'] = $weenk;
break;
}
}
}
这里一切正常,阵中没有任何事情发生
但稍后在这里
foreach ($users as $index => $user)
{
foreach ($newWeenks as $weenk)
{
if ($user['id'] == $weenk['userId'])
{
$users[$index]['weenk'] = $weenk;
break;
}
}
}
它为$ weenk
提供了错误未定义索引userId我已经检查过newWeenks是EMPTY返回它给[]然后它怎么进入循环?为什么我在这里得到这个错误而不是在上面的循环?
我的表格包含id userId weeenkedId type
答案 0 :(得分:1)
你的init数组设置不好,你有:$newWeenks[] = array();
您必须拥有:$newWeenks = array();