如何比较数组键的值并将新数组元素插入第一个数组?

时间:2014-03-18 06:20:02

标签: php arrays array-key

我有两个数组如下:

$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
        )

)

和第二个数组如下:

 $LastSendNewsletterDetail = 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 )相互比较。如果它们彼此匹配,则将数组$ LastSendNewsletterDetail中的数据插入第一个数组$grid_data。如果它们不匹配,则将空白值插入第一个数组$grid_data

例如,所需的最终$ 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
        )

)

如何实现这一目标?提前谢谢。

1 个答案:

答案 0 :(得分:-1)

试试这个

for($i=0; $i < $grid_data.lenght; $i++)
{
  $track =0;
  foreach($LastSendNewsletterDetail 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
  }
}