0

我有两个数组

$array1 = array(0=>array('mac'=>'mac0','geo'=>'geo0'),1=>array('mac'=>'mac1','geo'=>'geo1'),
2=>array('mac'=>'mac2','geo'=>'geo2')...);


$array2 = array(0=>array('id'=>'id0','gname'=>'gname0'),1=>array('id'=>'id1','gname'=>'gname1'),
2=>array('id'=>'id2','gname'=>'gname2')...);

我想比较第一个数组值 mac 和第二个数组值 gname 的两个数组

如果 geo 的值在 gname 的值中可用,则 update else do note update else do

不更新

例如,如果数组的值在 array2 中的任何数组中可用,则 UPDATE

否则不更新

4

1 回答 1

0

这里写了代码:

    $array1 = array(0=>array('mac'=>'mac0','geo'=>'geo0'),1=>array('mac'=>'mac1','geo'=>'geo1'),
2=>array('mac'=>'mac2','geo'=>'geo2'));


$array2 = array(0=>array('id'=>'id0','gname'=>'gname0'),1=>array('id'=>'id1','gname'=>'gname1'),
2=>array('id'=>'id2','gname'=>'gname2'));

function gluearray($ar1, $ar2) {
    foreach($ar2 as $key=>$value) {
        if(isset($ar1[$key])) {
            if(is_array($ar1[$key])) {
                $ar1[$key] = gluearray($ar1[$key], $value);
            } else {
                if($ar1[$key]!==$value) {
                    $ar1[$key] = $value;
                }
            }
        } else {
            $ar1[$key] = $value;
        }
    }
    return $ar1;
}
print_r(gluearray($array1, $array2));

结果:

数组 ( [0] => 数组 ( [mac] => mac0 [geo] => geo0 [id] => id0 [gname] => gname0 ) [1] => 数组 ( [mac] => mac1 [geo] => geo1 [id] => id1 [gname] => gname1 ) [2] => 数组 ( [mac] => mac2 [geo] => geo2 [id] => id2 [gname] => gname2 )

于 2012-06-05T14:17:53.073 回答