PHP100 2019-03-27
代码如下:
$test1 = Array( 0 => Array( 'id' => 9478137, 'create_time' => 1394760724 ), 1 => Array( 'id' => 9478138, 'create_time' => 1394760725 ), 2 => Array( 'id' => 9478138, 'create_time' => 1394760725 ) ); $test2 = array( 0 => array( 'id' => 9478137, 'message' => 'love you' ), 1 => array( 'id' => 9478138, 'message' => 'miss you' ) );
代码如下:
function _mergerArray($array1, $array2, $field1, $field2 = '') { $ret = array(); foreach($array1 as $key1 => $value1 ) { foreach ($array2 as $key2 => $value2) { if($value1[$field1] == $value2[$field2]) { $ret[$key1] = array_merge($value1, $value2); } } } return $ret; }
代码如下:
$test1 = Array( 0 => Array( 'id' => 9478137, 'create_time' => 1394760724 ), 1 => Array( 'id' => 9478138, 'create_time' => 1394760725 ), 2 => Array( 'id' => 9478138, 'create_time' => 1394760725 ) ); $test2 = array( 0 => array( 'id' => 9478137, 'message' => 'love you' ), 1 => array( 'id' => 9478138, 'message' => 'miss you' ) ); function _mergerArray($array1, $array2, $field1, $field2 = '') { $ret = array(); //使用数组下标的办法 foreach ($array2 as $key => $value) { $array3[$value[$field1]] = $value; } foreach ($array1 as $key => $value) { $ret[] = array_merge($array3[$value[$field1]], $value); } return $ret; } $ret = _mergerArray($test1, $test2, 'id', 'id'); print_r($ret);exit;
代码如下:
Array ( [0] => Array ( [id] => 9478137 [message] => love you [create_time] => 1394760724 ) [1] => Array ( [id] => 9478138 [message] => miss you [create_time] => 1394760725 ) [2] => Array ( [id] => 9478138 [message] => miss you [create_time] => 1394760725 ) )