选项1:
$foo = array($obj1, $obj2, $obj3);
for ($i = 0; $i < 1; $i++) {
echo $foo[$i]->attribute;
echo $foo[$i]->attribute2;
}
//shows obj1's attribute and attribute2
选项 2:
$foo = array($obj1, $obj2, $obj3);
$first_foo = array_shift($foo); /* now we only have the first index in the new array */
foreach ($first_foo as $object) {
echo $object->attribute;
echo $object->attribute2;
}
//shows obj1's attribute and attribute2
选项 3:
$foo = array($obj1, $obj2, $obj3);
$first_foo = $foo[0] /* now we just have an object, obj1 */
echo $first_foo->attribute;
echo $first_foo->attribute2;
//shows obj1's attribute and attribute2
我使用了选项 3,但所有这些都感觉有点缺乏......你会怎么做?如果您想轻松拉出前两个而不是稍后拉出一个,那么选项 1 和 2 中的循环是否值得?即拉最新的新闻文章与拉最新的两个等。