所选答案提供了大量的错误信息。我有一个理解如何使比较更严格使代码工作的问题。你会认为恰恰相反。
PHP手册:
$a == $b Equal TRUE if $a is equal to $b after type juggling.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4)
我还在我的回答立场 - 我想我是在正确的轨道上......但我们永远不会知道。
我相信问题出在你代码的else
部分。
我重新格式化了代码,这样我就可以确保你没有任何不匹配大括号的问题(你没有)。
请尝试下面的代码。仅供参考 - 我删除了$ id2,因为您没有在您提供的代码中使用它。
echo "Looking for: {$fbID}\n";
$photos = '';
for($i=0; $i<=count($photoData['data']); $i++)
{
$id = $photoData['data'][$i]['tags']['data'][0]['id'];
echo $id . ($id == $fbID)?' matches':' DOES NOT MATCH' . "<br>\n";
if($id == $fbID)
{
$photos .= "<div style='float:left;margin:25px;'><a href='crop.php?url=".$photoData['data'][$i]['source']."'><img src='".$photoData['data'][$i]['picture']."'/></a></div>";
}
else
{
$photos = 'We cant find any photo tagged with you, go back and select another album';
break; // if you don't break, then you're possibly overwriting anything that's been added in the "true" portion of the if statement.
}
}
[编辑]
两个循环将有助于在这里。可能有一种方法可以将其重构为单个循环,但这应该起作用。
echo "Looking for: {$fbID}<br>\n";
$items = array();
for($i=0; $i<=count($photoData['data']); $i++)
{
$id = $photoData['data'][$i]['tags']['data'][0]['id'];
echo $id . ($id == $fbID)?' matches':' DOES NOT MATCH' . "<br>\n";
if($id == $fbID)
{
$items[] = $photoData['data'][$i];
}
}
$photos = 'We cant find any photo tagged with you, go back and select another album';
if (count($items))
{
foreach($items)
{
$photos .= '<div style="float:left;margin:25px;"><a href="crop.php?url=' . $item['source'] . '"><img src="' . $item['picture'] . '"/></a></div>';
}
}
echo $photos;
你知道你正在改变代码第二行'$ id'的值,对吧? –
@Emil Vikstrom $ id2,它不是$ id。 –
使用'var_dump($ id);'和'var_dump($ fbID);'来说明这一点,而不是你认为变量包含的内容。 – mario