以下代码块为数组中的每个元素打印FOUND和NOT FOUND。我错过了什么?
$values = DB::query($SQL);
print_r($values);
foreach($values as $value)
{
$myVal = $value->thisAttribute;
if(isset($myVal)) print("FOUND");
if(!isset($myVal)) print("NOT FOUND");
}
输出:
Array
(
[0] => stdClass Object
(
[thisAttribute] =>
)
[1] => stdClass Object
(
[thisAttribute] => value
)
[2] => stdClass Object
(
[thisAttribute] =>
)
)
FOUNDNOTFOUNDFOUNDNOTFOUNDFOUNDNOTFOUND
答案 0 :(得分:5)
我想这样做的正确方法是:
foreach($values as $value)
{
if(isset($value->thisAttribute)) {
$myVal = $value->thisAttribute;
print("FOUND");
} else
print("NOT FOUND");
}