PHP头条
热点:

array_column 处理对象数组的办法


例如像下面这种由对象组成的数组($vipUserList),在php 7下可以通过 array_column 获取该数组的 parent_uid 的一个一维数组列表,但是在 5.6 中 array_column 却不能作用于对象。

Array
(
    [32648321] => App\Models\VipUser Object
        (
            [table:protected] => vip_user
            [mc_prefix:protected] => VipUser::
            [fillable:protected] => Array
                (
                    [0] => id
                    [1] => root_uid
                    [2] => parent_uid
                    [3] => vip_level
                    [4] => total_points
                    [5] => valid_points
                    [6] => is_welcome
                    [7] => recommend_time
                    [8] => update_level_time
                )

            [dateFormat:protected] => U
            [dates:protected] => Array
                (
                    [0] => expire_time
                    [1] => ctime
                    [2] => utime
                    [3] => dtime
                    [4] => deadline
                    [5] => is_send
                )

            [cache_time:protected] => Carbon\Carbon Object
                (
                    [date] => 2020-11-11 10:57:45.000000
                    [timezone_type] => 3
                    [timezone] => PRC
                )

            [cache_prefix:protected] => App\Models\VipUser:
            [cache_prefix_with_trashed:protected] => App\Models\VipUser:WithTrashed:
            [connection:protected] => 
            [primaryKey:protected] => id
            [keyType:protected] => int
            [perPage:protected] => 20
            [incrementing] => 1
            [timestamps] => 1
            [attributes:protected] => Array
                (
                    [id] => 32648321
                    [root_uid] => 0
                    [parent_uid] => 32546735
                    [vip_level] => 1
                    [total_points] => 2000
                    [valid_points] => 0
                    [is_welcome] => 0
                    [own_percent] => 10
                    [buyer_percent] => 0
                    [recommend_time] => 1602726516
                    [update_level_time] => 1602726517
                    [ctime] => 1602726516
                    [utime] => 1602726545
                    [dtime] => 
                )

            [original:protected] => Array
                (
                    [id] => 32648321
                    [root_uid] => 0
                    [parent_uid] => 32546735
                    [vip_level] => 1
                    [total_points] => 2000
                    [valid_points] => 0
                    [is_welcome] => 0
                    [own_percent] => 10
                    [buyer_percent] => 0
                    [recommend_time] => 1602726516
                    [update_level_time] => 1602726517
                    [ctime] => 1602726516
                    [utime] => 1602726545
                    [dtime] => 
                )

            [relations:protected] => Array
                (
                )

            [hidden:protected] => Array
                (
                )

            [visible:protected] => Array
                (
                )

            [appends:protected] => Array
                (
                )

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

            [casts:protected] => Array
                (
                )

            [touches:protected] => Array
                (
                )

            [observables:protected] => Array
                (
                )

            [with:protected] => Array
                (
                )

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

)

这里可以通过自己写函数处理一下:

$parent_uid_list = array_unique(array_values(array_map(function ($vipUser) {
        return $vipUser->parent_uid;
    }, $vipUserList)
));

www.phpzy.comtrue/php/38218.htmlTechArticlearray_column 处理对象数组的办法 例如像下面这种由对象组成的数组($vipUserList),在php 7下可以通过 array_column 获取该数组的 parent_uid 的一个一维数组列表,但是在 5.6 中 array_column 却不能...

相关文章

PHP之友评论

今天推荐