【问题标题】:building an array from looping an object通过循环对象构建数组
【发布时间】:2015-10-18 03:22:27
【问题描述】:

我目前正在开发一个 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
    );
}

然而,这会构建错误的结构并覆盖最后一个循环。

【问题讨论】:

    标签: php arrays object laravel eloquent


    【解决方案1】:

    你很聪明,你必须遍历你的结果数组,但是像这样:

    // 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",
                ...
            )
        ),
        ...
    )
    

    【讨论】:

      【解决方案2】:

      您在最后一个循环中覆盖数据的原因是因为您将新创建的数组分配给 $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);
      

      这应该会为您提供一个列表,其中包含需要发送的每封电子邮件的一个条目。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-07-20
        • 1970-01-01
        • 2020-05-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-27
        相关资源
        最近更新 更多