在比较两个数组中常见的键值之后,如何将第二个数组的某些字段合并到第一个数组中?

时间:2014-03-18 07:14:27

标签: php arrays associative-array key-value array-merge

我的问题听起来有些令人困惑,但实际上很简单。让我先描述一下这个场景。我有两个阵列即。 $grid_data$sent_data。两个阵列都根据用户输入动态生成。因此,例如我打印在这两个数组的内容之下:

$grid_data = Array
    (
        [0] => Array
            (
                [newsletter_id] => 1
                [newsletter_name] => Eywa Solutions
                [newsletter_subject] => Holi Wishes
                [newsletter_email_body] => Happy Holi to all the friends
                [newsletter_call_to_action_status] => 0
                [newsletter_call_to_action_text] => 
                [newsletter_call_to_action_link] => 
                [newsletter_status] => 2
                [newsletter_schedule] => 0
                [newsletter_created_date] => Mar 17 2014, 16:21 pm
                [newsletter_updated_date] => 0
            )

        [1] => Array
            (
                [newsletter_id] => 2
                [newsletter_name] => Akshay Nikte
                [newsletter_subject] => The Don
                [newsletter_email_body] => How are yoy Nikte Saheb?
                [newsletter_call_to_action_status] => 0
                [newsletter_call_to_action_text] => 
                [newsletter_call_to_action_link] => 
                [newsletter_status] => 2
                [newsletter_schedule] => 0
                [newsletter_created_date] => Mar 18 2014, 06:52 am
                [newsletter_updated_date] => 0
            )

    )

$sent_data = Array
        (
            [0] => Array
                (
                    [newsletter_id] => 1
                    [newsletter_sent_count] => 5
                    [newsletter_sent_date] => 1395121193
                )

            [1] => Array
                (
                    [newsletter_id] => 2
                    [newsletter_sent_count] => 7
                    [newsletter_sent_date] => 1395121227
                )

        )

现在我想要实现的是将上述两个数组中的键[newsletter_id]的值相互比较。如果找到匹配,则将键值对([newsletter_sent_count],[newsletter_sent_date])附加到$grid_data数组的相应数组中。如果未找到匹配,则为这些键插入空白值([newsletter_sent_count],[newsletter_sent_date])。在上面的例子中,最终生成的$grid _data应如下所示:

$grid_data = Array
    (
        [0] => Array
            (
                [newsletter_id] => 1
                [newsletter_name] => Eywa Solutions
                [newsletter_subject] => Holi Wishes
                [newsletter_email_body] => Happy Holi to all the friends
                [newsletter_call_to_action_status] => 0
                [newsletter_call_to_action_text] => 
                [newsletter_call_to_action_link] => 
                [newsletter_status] => 2
                [newsletter_schedule] => 0
                [newsletter_created_date] => Mar 17 2014, 16:21 pm
                [newsletter_updated_date] => 0
                [newsletter_sent_count] => 5
                [newsletter_sent_date] => 1395121193
            )

        [1] => Array
            (
                [newsletter_id] => 2
                [newsletter_name] => Akshay Nikte
                [newsletter_subject] => The Don
                [newsletter_email_body] => How are yoy Nikte Saheb?
                [newsletter_call_to_action_status] => 0
                [newsletter_call_to_action_text] => 
                [newsletter_call_to_action_link] => 
                [newsletter_status] => 2
                [newsletter_schedule] => 0
                [newsletter_created_date] => Mar 18 2014, 06:52 am
                [newsletter_updated_date] => 0
                [newsletter_sent_count] => 7
                [newsletter_sent_date] => 1395121227
            )

    )

我想以最佳方式做到这一点。我尝试使用array_merge()来实现这一点,但我的尝试并没有成功。那么人才库中的任何人都可以帮助我吗?提前致谢。如果您需要任何进一步的信息,我可以提供相同的信息。 我的尝试如下:

$arr_length = count($grid_data);


for($i=0; $i < $arr_length; $i++) {
  $track =0;
  foreach($sent_data as $value) {
    if($value['newsletter_id'] === $grid_data[$i]['newsletter_id'] ) {
      array_merge($grid_data[$i], $value);
      $track=1;
    }  
  }
  if($track === 0) {
    //insert blank value here
  }
}

1 个答案:

答案 0 :(得分:0)

首先将$sent_data转换为关联数组:

$sent_assoc = array();
foreach ($sent_data as $el) {
    $sent_assoc[$el['newsletter_id']] = $el;
}

然后进行合并:

$default_sent = array('newsletter_sent_count' => null, 'newsletter_sent_date' => null);
foreach ($grid_data as &$el) {
    $id = $el['newsletter_id'];
    $sent = isset($sent_assoc[$id]) ? $sent_assoc[$id] : $default_sent;
    $el = array_merge($el, $sent);
}

使用引用&$el,以便赋值更新原始数组。

没有引用,第二个循环看起来像:

foreach ($grid_data as $i => $el) {
    $id = $el['newsletter_id'];
    $sent = isset($sent_assoc[$id]) ? $sent_assoc[$id] : $default_sent;
    $grid_data[$i] = array_merge($el, $sent);
}