duanpeng1532 2012-10-03 07:34
浏览 62
已采纳

Php两个不同的数组,具有相同的键

I am new to php and still learning the language,

let say I have two array

For Example

Array
(
    [house_id] => 6
    [name] => Lake Villa
    [floor] => 5
    [unit] => 25
)

Array
(
    [house_id] => 6
    [name] => Lake Villa
    [floor] => 5
    [unit] => 25
    [parking_id] => 9
    [resident_count] => 4
)

How do i get the keys of 1st array onto second, what i am saying is, i just need house_id, name, floor, unit from second array and discard rest of the information.

However, they key is not same and dynamic, which means the first array key whatever returned is also present on second but with additional information. The information above is just an example and the keys might varies but whatever key on first array contains on second array too.

I tried this, but isn't working:

foreach($arr1 as $k=>$v) {
    foreach($arr2 as $j=>$w) {
        if(isset($arr2[$k]))
            $arr[$k] = $w;
    }
}
  • 写回答

6条回答 默认 最新

  • douzhuozhu9544 2012-10-03 07:36
    关注

    You could use array_intersect_key, to merge the arrays.

    $newArray = array_intersect_key($array2, $array1);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法