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条)

报告相同问题?