http://ideone.com/hiCRZ
$a = array(
array('ID'=>123 ,'VALUE'=>'123 value'),
array('ID'=>222 ,'VALUE'=>'222 value')
);
$b = array(
array('ID'=>123 ,'NAME'=>'123 name'),
array('ID'=>124 ,'NAME'=>'124 name'),
array('ID'=>125 ,'NAME'=>'125 name')
);
$c = array();
foreach( $a as $row ){
$c[ $row['ID'] ]['ID'] = $row['ID']; // stupid way to ensure target array :)
$c[ $row['ID'] ] += $row;
}
foreach( $b as $row ){
$c[ $row['ID'] ]['ID'] = $row['ID'];
$c[ $row['ID'] ] += $row;
}
var_dump( $c );
结果:
array(4) {
[123]=>
array(3) {
["ID"]=>
int(123)
["VALUE"]=>
string(9) "123 value"
["NAME"]=>
string(8) "123 name"
}
[222]=>
array(2) {
["ID"]=>
int(222)
["VALUE"]=>
string(9) "222 value"
}
[124]=>
array(2) {
["ID"]=>
int(124)
["NAME"]=>
string(8) "124 name"
}
[125]=>
array(2) {
["ID"]=>
int(125)
["NAME"]=>
string(8) "125 name"
}
}
如果要查找 1 个 id 的数据:
$id = 123;
$result_row = array();
foreach( $a as $row ) if( $row['ID'] == $id ) {
$result_row += $row;
}
foreach( $b as $row ) if( $row['ID'] == $id ){
$result_row += $row;
}
var_dump( $result_row );
结果:
array(3) {
["ID"]=>
int(123)
["VALUE"]=>
string(9) "123 value"
["NAME"]=>
string(8) "123 name"
}