根据键和值匹配两个数组

时间:2013-06-03 14:11:44

标签: php arrays

一个阵列有:

array(2) {
  [0]=> array(6) {
    ["sid"]=> string(1) "1"
    ["book_id"]=> string(3) "b_1"
    ["book_name"]=> string(9) "XYZ"
    ["time_added"]=> string(19) "2013-05-07 04:17:03"
    ["priority"]=> string(1) "1"
  }
  [1]=> array(6) {
    ["sid"]=> string(1) "2"
    ["book_id"]=> string(3) "b_2"
    ["book_name"]=> string(6) "TOP"
    ["time_added"]=> string(19) "2012-11-06 08:09:56"
    ["priority"]=> string(1) "1"
  }
}

第二个:

array(1) {
  [0]=> array(15) {
    ["book_id"]=> string(3) "625"
    ["b_1"]=> string(1) "6"
    ["b_2"]=> string(1) "11"
    ["date_book_added"]=> NULL
    ["category"]=> string(7) "nv"
  }
}

我如何制作第三个数组,其中键,b_1和b_2的值将被第二个数值替换:6和11.并且所有内容都将合并?

1 个答案:

答案 0 :(得分:0)

$array3 = array_merge($array1, $array2);

如果你想在一个数组数组上迭代地执行它

$results = array();
foreach($array1 as $k => $v){
    $results[$k] = array_merge($v, $array3);
}