4

I'm chasing my tail trying to combine the results of two different queries to output in a template.

I'm trying to merge the corresponding sub-arrays in model_data and entry_data to get desired_result. I will then iterate over desired_result and print values into the template.

Any assistance is greatly appreciated.

model_data

array(2) {
  [0]=>
  array(2) {
    ["entry_id"]=> string(3) "192"
    ["field_id_49"]=> string(10) "Model Name"
  }
  [1]=>
  array(2) {
    ["entry_id"]=> string(3) "193"
    ["field_id_49"]=> string(5) "MN123"
  }
}

entry_data

array(2) {
  [0]=>
  array(2) {
    ["uri"]=> string(24) "/products/product-title/"
    ["title"]=> string(13) "Product Title"
  }
  [1]=>
  array(2) {
    ["uri"]=> string(22) "/products/lorem-ipsum/"
    ["title"]=> string(11) "Lorem Ipsum"
  }
}

desired_result

array(2) {
  [0]=>
  array(4) {
    ["entry_id"]=> string(3) "192"
    ["field_id_49"]=> string(10) "Model Name"       
    ["uri"]=> string(24) "/products/product-title/"
    ["title"]=> string(13) "Product Title"      
  }
  [1]=>
  array(4) {
    ["entry_id"]=> string(3) "193"
    ["field_id_49"]=> string(5) "MN123"      
    ["uri"]=> string(22) "/products/lorem-ipsum/"
    ["title"]=> string(11) "Lorem Ipsum"
  }
}
Judd Lyon
  • 538
  • 1
  • 4
  • 11
  • Are you sure the desired result is correct? I can't actually understand the logic. – Narcis Radu Sep 27 '11 at 21:07
  • I need to merge model_data[0] with entry_data[0], model_data[1] with entry_data[1]. Both will continue to have arrays added to them, so the array key needs to increment. – Judd Lyon Sep 27 '11 at 21:11

2 Answers2

8
foreach($model_data as $key => $value){
    $result[$key] = array_merge($entry_data[$key], $model_data[$key]);
}
amosrivera
  • 26,114
  • 9
  • 67
  • 76
0

You can use array_replace_recursive function to merge these arrays. Below is a example:

$desired_result = array_replace_recursive($model_data, $entry_data);

var_dump($desired_result);
Jasmel Singh
  • 111
  • 1
  • 6