加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 合并两个多维关联数组

发布时间:2020-12-13 17:39:23 所属栏目:PHP教程 来源:网络整理
导读:我正在追逐我的尾巴试图将两个不同查询的结果组合在一个模板中输出. 我正在尝试合并model_data和entry_data中的相应子数组以获取desired_result.然后,我将遍历desired_result并将值打印到模板中. 非常感谢任何帮助. model_data array(2) { [0]= array(2) { [
我正在追逐我的尾巴试图将两个不同查询的结果组合在一个模板中输出.

我正在尝试合并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]);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读