开发者

match array values

I have two arrays like this:

[a] => Array
    (
        [w] => 90
        [h] => 90       
    )

[b] => Array
    (
        [w] => 40
        [h] => 25         
    )

And:

[a] => Array
    (
        [w] => 90
        [h] => 90   开发者_JAVA技巧 
        [name]  => test
    )

[b] => Array
    (
        [w] => 40
        [h] => 25         
        [name]  => test2
    )

How can I find out if the W and H elements from the 1st array match the ones from the second one, and if they do get the name value of the matched element ?


// $arrayWithNamesInIt is the array with w, h, and name
// $array1 is the array with only w and h

$names = array(); // Will hold the names of matching entries
foreach ($arrayWithNamesInIt as $key => $values) {
    if (isset($array1[$key]) && $values['w'] == $array1[$key]['w'] && $values['h'] == $array1[$key]['h']) {
        $names[] = $values['name'];
    }
}

(demo)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜