How to merge some fields of second array into first array after comparing the values of keys common in both arrays?

StackOverflow https://stackoverflow.com/questions/22472611

Question

My question sounds bit confusing but actually it's simple. Let me describe you the scenario first. I've two arrays viz. $grid_data and $sent_data. Both the arrays generate dynamically depending on the user input. So for instance I'm printing below the contents of both of these arrays:

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

        )

Now what I want to achieve is compare the values of key [newsletter_id] from both of the above arrays with each other. If match is found append the key-value pairs([newsletter_sent_count], [newsletter_sent_date]) to the respective array from $grid_data array. If match is not found then insert blank values for those keys([newsletter_sent_count], [newsletter_sent_date]). In above case the finally generated $grid _data should look like below:

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

    )

I want to do this in an most optimum way. I tried using array_merge() to achieve this but I was not succeed in my attempt. So can anyone from the pool of talent help me in this regard please? Thanks in advance. If you need any further information I can provide you the same. The attempt made by me is as follows:

$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
  }
}
Was it helpful?

Solution

First convert $sent_data into an associative array:

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

Then do the merging:

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

The reference &$el is used so that the assignment updates the original array.

Without the reference the second loop looks like:

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top