繁体   English   中英

合并两个多维关联数组

[英]Merging two multidimensional associative arrays

我正在追逐我的尾巴试图将两个不同查询的结果组合在一个模板中输出。

我正在尝试合并model_data和entry_data中的相应子数组以获取desired_result。 然后,我将遍历desired_result并将值打印到模板中。

非常感谢任何帮助。

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"
  }
}
foreach($model_data as $key => $value){
    $result[$key] = array_merge($entry_data[$key], $model_data[$key]);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM