访问模型,无需输入


Access model without foreach

如果可以在foreach循环中访问entry_id"数据,如下所示,如果我知道只有一个项目,有没有更好的方法来访问它?

$arr = array();
foreach( $order->items as $item ) {
    $arr[] = $item->entry_id;
}

如果 order->items 是无关联数组,并且只有 1 个元素,您可以通过以下方式访问它:

echo $order->items[0]->entry_id;

但更安全的是检查数组中有多少项目:

if(count($order->items) > 0)
    echo $order->items[0]->entry_id;