我正在研究一个laravel应用程序,我正在尝试从eloquent从数据库返回的对象创建一个数组。对象看起来像这样,
[
{
"id": 3,
"body": "Fake User created the project Fake Project 2",
"uri": "8jss90xk",
"object_id": 422,
"user": [
{
"id": 25,
"email": "xxxxxxxxxx@gmail.com",
"first_name": "xxx1",
"last_name": "xxxx1",
"display_name": "xxxxxxxxxxx",
"initials": "XX",
"remember_me": null,
"active": "1",
"invite_code": null,
"forgotten_code": null,
"cost_visible": 0,
"login_type": "normal",
"api_token": null,
"created_at": "2015-02-13 11:47:24",
"updated_at": "2015-05-06 22:43:23",
"deleted_at": null,
"pivot": {
"notification_id": 3,
"user_id": 25,
"is_read": 0
}
}
],
"project": {
"id": 422,
"name": "Project Number 1",
"slug": "Tdc3de97",
"uri_hash": "project-number-1",
"description": null,
"total_cost": "1000.00",
"start_date": "2015-07-21",
"finish_date": "2015-10-22",
"status": 2,
"sales_person": null,
"client_id": 0,
"organisation_id": 97,
"owner_id": 97,
"user_id": 25,
"locked_by": null,
"created_at": "2015-07-21 13:39:21",
"updated_at": "2015-07-24 10:07:38",
"deleted_at": null,
"archived_at": "0000-00-00 00:00:00",
"invoiced_at": null,
"is_internal": 0
}
},
{
"id": 4,
"body": "Fake User created the project Fake Project 3",
"uri": "8jss90xk",
"object_id": 422,
"user": [
{
"id": 25,
"email": "XXXXXX@gmail.com",
"first_name": "XXXXX",
"last_name": "XXXXXXXX",
"display_name": "XXX",
"initials": "XX",
"remember_me": null,
"active": "1",
"invite_code": null,
"forgotten_code": null,
"cost_visible": 0,
"login_type": "normal",
"api_token": null,
"created_at": "2015-02-13 11:47:24",
"updated_at": "2015-05-06 22:43:23",
"deleted_at": null,
"pivot": {
"notification_id": 4,
"user_id": 25,
"is_read": 0
}
}
],
"project": {
"id": 422,
"name": "Project Number 1",
"slug": "Tdc3de97",
"uri_hash": "project-number-1",
"description": null,
"total_cost": "1000.00",
"start_date": "2015-07-21",
"finish_date": "2015-10-22",
"status": 2,
"sales_person": null,
"client_id": 0,
"organisation_id": 97,
"owner_id": 97,
"user_id": 25,
"locked_by": null,
"created_at": "2015-07-21 13:39:21",
"updated_at": "2015-07-24 10:07:38",
"deleted_at": null,
"archived_at": "0000-00-00 00:00:00",
"invoiced_at": null,
"is_internal": 0
}
},
{
"id": 5,
"body": "Fake User created the project Fake Project 4",
"uri": "8jss90xk",
"object_id": 422,
"user": [
{
"id": 25,
"email": "XXXXXXX@gmail.com",
"first_name": "XXXXXX",
"last_name": "XXXXXXX",
"display_name": "XXXXXXX",
"initials": "XX",
"remember_me": null,
"active": "1",
"invite_code": null,
"forgotten_code": null,
"cost_visible": 0,
"login_type": "normal",
"api_token": null,
"created_at": "2015-02-13 11:47:24",
"updated_at": "2015-05-06 22:43:23",
"deleted_at": null,
"pivot": {
"notification_id": 5,
"user_id": 25,
"is_read": 0
}
}
],
"project": {
"id": 422,
"name": "Project Number 1",
"slug": "Tdc3de97",
"uri_hash": "project-number-1",
"description": null,
"total_cost": "1000.00",
"start_date": "2015-07-21",
"finish_date": "2015-10-22",
"status": 2,
"sales_person": null,
"client_id": 0,
"organisation_id": 97,
"owner_id": 97,
"user_id": 25,
"locked_by": null,
"created_at": "2015-07-21 13:39:21",
"updated_at": "2015-07-24 10:07:38",
"deleted_at": null,
"archived_at": "0000-00-00 00:00:00",
"invoiced_at": null,
"is_internal": 0
}
}
}]
基本上,查询会返回所有未读通知的用户,我想将其与任何通知一起构建到数组中并发送电子邮件,但数组需要非常具体,包含其中包含的详细信息。
我想将这样的内容发送到将发送提醒的邮件功能
'user@emailaddress.com' => array(
'first_name' => 'User',
'last_name' => 'Lastname',
'projects' => array(
'Project Title 1' => array(
'notifications' => array(
[0] => 'Notification 1',
[1] => 'Notification 2',
[2] => 'Notification 3'
)
)
)
)
我不能为我的生活找出如何从我的对象构建这个数组,我目前在我的代码中有这个,
foreach($unread as $object) {
$emails_to_send = array(
'user' => $object->user[0],
'uri' => $object->uri,
'body' => $object->body,
'project' => $object->project
);
}
然而,这会构建错误的结构并覆盖最后一个循环。
答案 0 :(得分:1)
在最后一个循环中获取数据的原因是因为您将新创建的数组分配给 $ emails_to_send 变量而不是附加到数组。请尝试以下方法:
$emails_to_send = array();
foreach($unread as $object) {
$emails_to_send[] = array(
'user' => $object->user[0],
'uri' => $object->uri,
'body' => $object->body,
'project' => $object->project
);
}
var_dump($emails_to_send);
这应该为您提供一个列表,其中包含每个需要发送的电子邮件的一个条目。
答案 1 :(得分:0)
你是怀疑的,你必须遍历你得到的数组,但是像这样:
// First create the final array
$emails_to_send = array();
foreach($unread as $object) {
// Add new array element with the email key
$emails_to_send[$object->user[0]->email] = array(
'first_name' => $object->user[0]->first_name,
'last_name' => $object->user[0]->last_name,
'project' => $object->project
);
}
var_export($emails_to_send);
结果你会看到类似的东西:
array(
'user@emailaddress.com' => array(
'first_name' => 'User',
'last_name' => 'Lastname',
'project' => array(
"id" => 422,
"name" => "Project Number 1",
"slug" => "Tdc3de97",
"uri_hash" => "project-number-1",
...
)
),
...
)