如何将子数组[* attributes]提取到任何新数组中

时间:2016-07-14 10:00:57

标签: php arrays laravel

我有以下数组,它是来自对象的类型转换的结果。在将对象转换为数组后,我得到以下数组数据输出

 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] => aamir.shoebn39@otssolutions.com
        [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] => aamir.shoebn39@otssolutions.com
        [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] => 
)

我想将[* attributes]子数组提取到名为$ errordata []的新数组中。

我的代码如下,

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

但它显示错误,

  Undefined index: *attributes

非常感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分:0)

我得到了解决方案,我使用laravel中的toArray()方法提取[' *属性']数据。它会自动将对象数据转换为数组。

我正在使用

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

然后添加到阵列()。

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

感谢。