Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何循环对象并在关联数组中显示其数据_Php_Arrays_Laravel - Fatal编程技术网

Php 如何循环对象并在关联数组中显示其数据

Php 如何循环对象并在关联数组中显示其数据,php,arrays,laravel,Php,Arrays,Laravel,首先,这是我的代码: Log::create([ 'action' => $event->changes[0], //<- This is my problem 'object_id' => $event->id, 'object_type' => "Account", 'ip_address' => Request::ip(), 'user' => ucfirst(Auth::user()->na

首先,这是我的代码:

Log::create([

    'action' => $event->changes[0], //<- This is my problem

    'object_id' => $event->id,
    'object_type' => "Account",
    'ip_address' => Request::ip(),
    'user' => ucfirst(Auth::user()->name),
    'time' => Carbon::now()->format('H:i:s'),
    'date' => Carbon::now()->format('Y-m-d')
]);
Log::创建([
“操作”=>$event->changes[0],//$event->id,
“对象类型”=>“帐户”,
“ip_地址”=>请求::ip(),
'user'=>ucfirst(Auth::user()->name),
'time'=>Carbon::now()->格式('H:i:s'),
'date'=>Carbon::now()->格式('Y-m-d')
]);
$event->changes
是一个数组,其中包含许多项,但我只知道如何使用索引
[0]
[1]
等一次获取一个特定项

如何让所有的值一次显示而不是一个?很明显,我不想为每一个动作创建一个新的日志,但我不知道怎么做


如往常一样,我们非常感谢您的帮助。

如果您希望在一个日志条目中对$event->更改进行序列化,则需要对其进行序列化

它实际上取决于changes数组的结构,但它似乎是一个字符串数组,因此您可以例如使用内爆(',',$changes),因此代码段将如下所示:

Log::create([

    'action' => implode(', ', $event->changes), //<- This is my problem

    'object_id' => $event->id,
    'object_type' => "Account",
    'ip_address' => Request::ip(),
    'user' => ucfirst(Auth::user()->name),
    'time' => Carbon::now()->format('H:i:s'),
    'date' => Carbon::now()->format('Y-m-d')
]);
Log::创建([
“操作”=>内爆(“,”,$event->changes),//$event->id,
“对象类型”=>“帐户”,
“ip_地址”=>请求::ip(),
'user'=>ucfirst(Auth::user()->name),
'time'=>Carbon::now()->格式('H:i:s'),
'date'=>Carbon::now()->格式('Y-m-d')
]);

Try:
'action'=>内爆(“,”,$event->changes),
我试试看