Merging two multidimensional associative arrays

安稳与你 提交于 2019-12-06 04:34:29

问题


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"
  }
}

回答1:


foreach($model_data as $key => $value){
    $result[$key] = array_merge($entry_data[$key], $model_data[$key]);
}


来源:https://stackoverflow.com/questions/7575666/merging-two-multidimensional-associative-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!