我有两个数组,比如说:
数组1:
Array
(
[test1] => Array
(
[abbrev] => Test data
[title] => Test data
)
[test2] => Array
(
[abbrev] => Test data
[title] => Test data
)
[test3] => Array
(
[abbrev] => Test data
[title] => Test data
)
)
数组2:
Array
(
[test1] => Array
(
[abbrev] => Test data
[title] => Test data
)
[test3] => Array
(
[abbrev] => Test data
[title] => Test data
)
)
当数组1对数组2进行排序时,结果为:
Array
(
[test1] => Array
(
[abbrev] => Test data
[title] => Test data
)
[test3] => Array
(
[abbrev] => Test data
[title] => Test data
)
[test2] => Array
(
[abbrev] => Test data
[title] => Test data
)
)
因此test1和test3位于顶部,因为它们按照与数组2中相同的顺序排序,而test3放在底部,因为它不在数组2中。
有什么想法吗?
答案 0 :(得分:1)
这样可以正常工作:
$arrayOne = array('test1'=>array(1, 2, 3),
'test2'=>array(1, 2, 3),
'test3'=>array(1, 2, 3),
);
$arraySecond = array('test1'=>array(1, 2, 3),
'test'=>array(1, 2, 3),
);
foreach ($arraySecond as $key => $arrSecond){
if (isset($arrayOne[$key])){
$res[$key]= $arrSecond;
unset ($arrayOne[$key]);
}
}
$res = array_merge($res, $arrayOne);
var_dump($res);
结果是:
["test1"]=>
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
["test3"]=>
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
["test2"]=>
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
}
答案 1 :(得分:1)
试试这个:
$array1 = array(...); // array 1
$array2 = array(...); // array 2
$result = array_merge(array_intersect_key($array2, $array1), array_diff_key($array1, $array2));