array_intersect_key($array1, $array2)
PHP マニュアル
コード:
$array1 = array('user1'=> array('name'=>'name1','age' => 'age1'),
'user2'=> array('name'=>'name2','age' => 'age2'),
'user3'=> array('name'=>'name3','age' => 'age3'));
//array with key to search for
$array2 = array('user2'=> array('time'=>'time2','date' => 'date2'));
echo '<pre>';
print_r(array_intersect_key($array1,$array2));
echo '</pre>';
o/p:
Array
(
[user2] => Array
(
[name] => name2
[age] => age2
)
)
編集:
ああ、問題があります。上記の構造に従って指定された配列を操作し、array_intersect_key() を適用する場合は、次のようにします。
function compare($key1,$key2){
global $array1,$array2;
if($array1[$key1]['userid'] == $array2[$key2]['userid'])
return 0;
if($array1[$key1]['userid'] > $array2[$key2]['userid'])
return 1;
else
return -1;
}
$array1 = array(array('userid'=>'user1','name'=>'name1','age' => 'age1'),
array('userid'=>'user2','name'=>'name2','age' => 'age2'),
array('userid'=>'user3','name'=>'name3','age' => 'age3'));
//array with key to search for
$array2 = array(array('userid'=>'user2','time'=>'time2','date' => 'date2'));
echo '<pre>';
print_r(array_intersect_ukey($array1,$array2,'compare'));
echo '</pre>';
o/p:
====
Array
(
[1] => Array
(
[userid] => user2
[name] => name2
[age] => age2
)
)