简体   繁体   English

PHP Laravel:如何访问数组上的受保护值

[英]PHP Laravel : How to Access Protected value on array

I have problem to get value of protected array in my laravel project and want to save my data into database using foreach.我在我的 laravel 项目中获取受保护数组的值时遇到问题,并且想使用 foreach 将我的数据保存到数据库中。 I used to print_r my data我曾经print_r我的数据

print_r($request->data);

Here is my array data:这是我的数组数据:

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 900
                    [zone_id] => 1
                    [account_id] => 2
                    [size] => 23474836488
                )

            [1] => stdClass Object
                (
                    [id] => 9001
                    [zone_id] => 2
                    [account_id] => 2
                    [size] => 23474836488
                )
        )
)

Is there any solution of my problem?我的问题有什么解决办法吗?

You are getting an array in the object.您将在 object 中获得一个数组。

You can access it as below.您可以按以下方式访问它。

foreach($request->data as $data){
    echo $data->id;
    echo $data->zone_id;
    echo $data->account_id;
    echo $data->size;
}

In Laravel, whenever you'll execute DB query or fetch records from database it will return you this kind of object.在 Laravel 中,每当你执行数据库查询或从数据库中获取记录时,它都会返回这种 object。

If you want to see the object to array the as per @Ammar Faizi Comment you can convert it into array.如果你想看到 object 按照@Ammar Faizi 评论排列,你可以将它转换成数组。 $request->data->toArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM