<?php
$array1 = array(
array(
'user_id' => 1,
'name' => 'foo'
),
array(
'user_id' => 2,
'name' => 'foobar'
)
);
$array2 = array(
array(
'user_id' => 2,
'name' => 'foobar'
),
array(
'user_id' => 3,
'name' => 'baz'
)
);
/**
* Return the common sub_array of tow arrays with tow dimensions
*
* @param $arrayA - the first tow dimension array
* @param $arrayB - the second tow dimension array
*
* @return array - an empty array in case the tow arrays don't
* have any common elements, otherwise an tow dimension array
* containing the common elements
*/
function get_common_array($arrayA, $arrayB) {
$result = array();
foreach($arrayA as $keyA=>$sub_arrayA) {
foreach($sub_arrayA as $metadata=>$data) {
if('user_id' === $metadata) {
foreach($arrayB as $keyB=>$sub_arrayB){
if(in_array($data, $sub_arrayB)) {
$result[] = $sub_arrayB;
}
}
}
}
}
return $result;
}
var_dump(get_common_array($array1, $array2));
这是一种解决方法,但如果我考虑改变拖曳数组的几何形状,以便能够使用 PHP 中实现的专用函数,那么这会更快。