访问存储在数组中的每个对象

时间:2013-03-13 17:09:43

标签: php laravel

我只想澄清如何使用存储在数组中的对象?在Laravel我触发查询。

$accounts = Account::where('customer_no', '=', $customer->customer_no)->get();

这会返回数组中的两个对象,这里是die和dump:

array(2) {
  [0]=>
   object(Account)#40 (5) {
 ["attributes"]=>
 array(11) {
  ["acc_no"]=>
  int(5)
  ["acc_type"]=>
  string(7) "CURRENT"
  ["start_date"]=>
  string(19) "2012-09-05 00:00:00"
  ["status"]=>
  int(1)
  ["balance"]=>
  string(5) "57.67"
  ["end_date"]=>
  NULL
  ["interest_rate"]=>
  string(4) "0.60"
  ["pin"]=>
  string(4) "1112"
  ["attempts"]=>
  int(2)
  ["bank_code"]=>
  int(1)
  ["customer_no"]=>
  int(10000003)
}
[1]=>
  object(Account)#43 (5) {
  ["attributes"]=>
  array(11) {
  ["acc_no"]=>
  int(6)
  ["acc_type"]=>
  string(7) "SAVINGS"
  ["start_date"]=>
  string(19) "2007-01-01 00:00:00"
  ["status"]=>
  int(1)
  ["balance"]=>
  string(7) "1002.01"
  ["end_date"]=>
  NULL
  ["interest_rate"]=>
  string(4) "0.80"
  ["pin"]=>
  string(4) "3427"
  ["attempts"]=>
  int(2)
  ["bank_code"]=>
  int(1)
  ["customer_no"]=>
  int(10000003)
}

所以我想单独访问这些对象,以便我可以使用它们,而不必单独访问它们。

如何在PHP中独立存储这些对象?

1 个答案:

答案 0 :(得分:1)

您可以从数组索引中获取对象。

$account_one = $accounts[0];
$account_two = $accounts[1];

您是否有理由不想使用foreach?您还可以使用常规for循环并使用$accounts[i]来引用特定帐户。

相关问题