2016-07-14 14 views
0

我有以下數組,它是來自對象的類型轉換的結果。我得到的數據陣列下面的輸出類型轉換對象數組後,如何將子數組[*屬性]提取到任何新的數組中

Array 
(
[*table] => inspector_details 
[*connection] => 
[*primaryKey] => id 
[*perPage] => 15 
[incrementing] => 1 
[timestamps] => 1 
[*attributes] => Array 
    (
     [id] => 94 
     [firstname] => Aamir39 
     [address1] => 
     [address2] => 
     [city] => 
     [state] => 
     [is_frozen] => 1 
     [email] => [email protected] 
     [is_email_private] => 0 
     [status] => 2 
     [latitude] => 
     [longitude] => 
     [created_at] => 2016-07-12 15:40:14 
     [updated_at] => 2016-07-13 11:40:44 
     [zip] => 0 
     [company] => OTS 
     [website] => 
     [phone] => 
     [is_phone_private] => 0 
     [company_began_date] => 0000-00-00 
     [logo] => 
     [freetext] => 
     [lic_name] => 
     [lic_id] => 
     [lic_url] => 
    ) 

[*original] => Array 
    (
     [id] => 94 
     [firstname] => Aamir39 
     [address1] => 
     [address2] => 
     [city] => 
     [state] => 
     [is_frozen] => 1 
     [email] => [email protected] 
     [is_email_private] => 0 
     [status] => 2 
     [latitude] => 
     [longitude] => 
     [created_at] => 2016-07-12 15:40:14 
     [updated_at] => 2016-07-13 11:40:44 
     [zip] => 0 
     [company] => OTS 
     [website] => 
     [phone] => 
     [is_phone_private] => 0 
     [company_began_date] => 0000-00-00 
     [logo] => 
     [freetext] => 
     [lic_name] => 
     [lic_id] => 
     [lic_url] => 
    ) 

[*relations] => Array 
    (
    ) 

[*hidden] => Array 
    (
    ) 

[*visible] => Array 
    (
    ) 

[*appends] => Array 
    (
    ) 

[*fillable] => Array 
    (
    ) 

[*guarded] => Array 
    (
     [0] => * 
    ) 

[*dates] => Array 
    (
    ) 

[*dateFormat] => 
[*casts] => Array 
    (
    ) 

[*touches] => Array 
    (
    ) 

[*observables] => Array 
    (
    ) 

[*with] => Array 
    (
    ) 

[*morphClass] => 
[exists] => 1 
[wasRecentlyCreated] => 
) 

我想提取[*屬性]子陣列中的新陣列名爲$ errordata子[]。

我的代碼如下,

$emaildataarray = (array)$emaildata; 
    array_push($errordata, $emaildataarray['*attributes']); 

,但它給我的錯誤,

Undefined index: *attributes 

任何幫助深表感謝。謝謝。

+0

是如何*屬性的陣列的一部分中描述當你'的var_dump($ emaildataarray)' – BeetleJuice

+0

@BeetleJuice喜歡這張 [ 「*屬性」] =>數組 (25){ [ 「ID」] = > int(94).................... – Aamir

+0

然後你的代碼'$ emaildataarray ['* attributes']'應該可以工作。當你的數據仍然是一個對象時,你如何訪問'* attributes'? – BeetleJuice

回答

0

我得到了解決方案,我使用laravel中的toArray()方法提取['* attributes']數據。它會自動將對象數據轉換爲數組。

我用

$emaildata = User::whereEmail($email)->first(); 

加入指定者()。

$emaildata = User::whereEmail($email)->first()->toArray(); 

謝謝。

+0

從這裏獲得解決方案http://laravel.io/forum/02-16-2014 -iterate-through-model-attributes – Aamir

+0

很高興你的工作。我會刪除我的答案。我建議您將代碼粘貼到您的答案中,以便稍後看到此代碼的人即使鏈接的頁面消失也會有解決方案。 – BeetleJuice

相關問題