匹配数组值

时间:2011-08-01 11:54:06

标签: php arrays

我有两个这样的数组:

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

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

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

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

如何判断第一个数组中的W和H元素是否与第二个数组中的W和H元素匹配,以及它们是否获得匹配元素的name值?

1 个答案:

答案 0 :(得分:-1)

// $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