我如何访问 laravel 中的数组元素?
how do i access elements of an array in laravel?
我正在尝试计算等于某个值的元素的数量,但是当我 运行:
时,我很难找到我想要比较的元素
{{dd($numberofnotifications)}}
我得到以下信息:
但我需要比较的值低于 'attributes'
那么如何获取属性下的值?
当我打印出数组的每个元素时,我得到以下格式:
{"id":"96a40ebb-a2d1-44d8-9600-e94dd026f152","type":"App\Notifications\CommentCreated","notifiable_type":"App\User","notifiable_id":1,"data":{"comment_id":9,"data":{"name":"John","date":"2020-12-30T08:37:47.000000Z","email":"John@gmail.com","post":2,"body":"test"},"message":"John commented on your post"},"read_at":null,"created_at":"2020-12-30T08:37:47.000000Z","updated_at":"2020-12-30T08:37:47.000000Z"}
由于结果是一个集合,您可以使用
转换它们
$numberofnotifications->toArray();
您需要使用 foreach 遍历集合:
foreach($numberofnotification as $number)
{
$number->name; //name here is the "name" of the attribute you want to access
}
如果您想将它们存储在一个数组中,那么:
foreach($numberofnotification as $number)
{
$attributes[]=$number->name; //name here is the "name" of the attribute you want to access
}
你试过这个吗
//假设$username为登录用户名
$filtered_count = $numberofnotifications->where('name', $username)->count();
如果你想要记录
$filtered = $numberofnotifications->where('name', $username)->all();
items 似乎是一个 DatabaseNotifications 数组。因此,您可以通过其索引手动获取每个元素:
$numberofnotifications[0]->id;
// or
$numberofnotifications[0]->type
或者您可以使用 foreach
循环:
foreach ($numberofnotifications as $notification) {
$notification->id;
}
我正在尝试计算等于某个值的元素的数量,但是当我 运行:
时,我很难找到我想要比较的元素{{dd($numberofnotifications)}}
我得到以下信息:
但我需要比较的值低于 'attributes'
那么如何获取属性下的值?
当我打印出数组的每个元素时,我得到以下格式:
{"id":"96a40ebb-a2d1-44d8-9600-e94dd026f152","type":"App\Notifications\CommentCreated","notifiable_type":"App\User","notifiable_id":1,"data":{"comment_id":9,"data":{"name":"John","date":"2020-12-30T08:37:47.000000Z","email":"John@gmail.com","post":2,"body":"test"},"message":"John commented on your post"},"read_at":null,"created_at":"2020-12-30T08:37:47.000000Z","updated_at":"2020-12-30T08:37:47.000000Z"}
由于结果是一个集合,您可以使用
转换它们$numberofnotifications->toArray();
您需要使用 foreach 遍历集合:
foreach($numberofnotification as $number)
{
$number->name; //name here is the "name" of the attribute you want to access
}
如果您想将它们存储在一个数组中,那么:
foreach($numberofnotification as $number)
{
$attributes[]=$number->name; //name here is the "name" of the attribute you want to access
}
你试过这个吗
//假设$username为登录用户名
$filtered_count = $numberofnotifications->where('name', $username)->count();
如果你想要记录
$filtered = $numberofnotifications->where('name', $username)->all();
items 似乎是一个 DatabaseNotifications 数组。因此,您可以通过其索引手动获取每个元素:
$numberofnotifications[0]->id;
// or
$numberofnotifications[0]->type
或者您可以使用 foreach
循环:
foreach ($numberofnotifications as $notification) {
$notification->id;
}