2013-03-13 88 views
-1

我只想清除如何使用存儲在數組中的對象?在Laravel中,我觸發了一個查詢。訪問存儲在數組中的每個對象

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

這以數組形式返回兩個對象這裏是一個模和轉儲:

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) 
} 

所以我要單獨訪問這些對象,所以我可以拿來用,而不必做foreach單獨訪問它們。

如何在PHP中獨立存儲這些對象?

回答

1

您可以從數組索引中獲取對象。

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

有你不想使用foreach雖然原因是什麼?您也可以使用常規的for循環並使用$accounts[i]來引用特定帳戶。