我有2个阵列(一个单一和一个多维)。
单阵列" A"看起来像
[questionid] => Array
(
[0] => 12
[1] => 13
[2] => 55
[3] => 15
[4] => 16
)
多维数组" B"看起来像
Array
(
[0] => Array
(
[quid] => 12
[answer] => AAA
)
[1] => Array
(
[quid] => 13
[answer] => neighbour
)
[2] => Array
(
[quid] => 15
[answer] =>
)
[3] => Array
(
[quid] => 16
[answer] =>
)
[4] => Array
(
[quid] => 55
[answer] =>
)
)
现在我希望根据数组A中的值重新排列数组B(quid)值。因此,在数组B中,quid最后一个元素(55)的值位于最后,而在数组A中,它是第三个位置。
我希望数组B看起来像这样
Array
(
[0] => Array
(
[quid] => 12
[answer] => AAA
)
[1] => Array
(
[quid] => 13
[answer] => neighbour
)
[2] => Array
(
[quid] => 55
[answer] =>
)
[3] => Array
(
[quid] => 15
[answer] =>
)
[4] => Array
(
[quid] => 16
[answer] =>
)
)
多维数组的代码是
$ansid = array
(
array
(
"quid" => 12,
"answer" => "AAA"
),
array
(
"quid" => 13,
"answer" => "neighbour"
),
array
(
"quid" => 15,
"answer" =>""
),
array
(
"quid" => 16,
"answer" =>""
),
array
(
"quid" => 55,
"answer" =>""
)
);
答案 0 :(得分:1)
不使用array_walk()
更具说明性,您可以
$newB=array()
foreach ($arrayB as $b) $newB[$b['quid']]=$b;
$newA=array()
foreach ($arrayA as $k=>$v) $newA[$k]=$newB[$v]
//$newA has the required structure
答案 1 :(得分:0)
使用用户排序功能:
$single_array = ...; // order by the index of this array
$mult_dim_array = ...; // to be ordered by the 'quid' value of the elements
function my_comp($a, $b) {
return array_search($a['quid'], $single_array ) - array_search($b['quid'], $single_array );
}
usort($mult_dim_array, "my_comp");
这将获得第一个数组的索引,以确定哪个元素首先或更晚。该函数将$single_array
读作全局变量(在函数外部定义)。