开发者

Compare two different arrays and unset element from the first one

Hey guys. I have a question. I have two different arrays with different structure and i want to compare the values and unset the common values. The first arrays looks like:

Array ( [0] => Array ( [key1] => value1 [key2] => value2 ) [1] => Array ( [key1] => value3 [key2] => value4 ) [2] => Array ( [key1] => value5 [key2] => value6 ) [3] => Array ( [key1] => value7 [key2] => value9 ) [4] => Array ( [key1] => value11 [key2] => value13 ))

The seco开发者_如何学运维nd array looks like:

Array ( [0] => value1 [1] => value3 [2] => value9)

So, i need to parse all the values from the first array and compare the first key with elements from the second array. Something like this

foreach($array1 as $ar1){
    foreach($array2 as $ar2){
        if($ar1['key1'] == $ar2){
            unset($array1[$ar1]);
        }
    }
}

I've tried everything but it's not working. The first array is generated so i can't change it's structure. The second one is made by hand. After the process, the first array will look like:

 Array ([2] => Array ( [key1] => value5 [key2] => value6 ) [3] => Array ( [key1] => value7 [key2] => value8 ))

Help me with some ideas. Thanks


Try this:

foreach($array1 as $k => $ar1){
    foreach($array2 as $ar2){
        if($ar1['key1'] == $ar2){
            unset($array1[$k]);
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜