我想根据$ArrayToOrder
中的列SecondArrayField2
对$SecondArray
进行排序,其中两个数组之间的链接为Field_3
(在$ ArrayToOrder中)和{{1 }}在$ SecondArray中。
SecondArrayField1
所需结果如下:
$ArrayToOrder=Array
(
[0] => Array
(
[Field_1] => 13
[Field_2] => 15
[Field_3] => 3
)
[1] => Array
(
[Field_1] => 25
[Field_2] => 17
[Field_3] => 2
)
[2] => Array
(
[Field_1] => 121
[Field_2] => 20
[Field_3] => 11
)
)
$SecondArray=Array
(
[0] => Array
(
[SecondArrayField1] => 11
[SecondArrayField2] => Bruce
)
[1] => Array
(
[SecondArrayField1] => 3
[SecondArrayField2] => Arthur
)
[2] => Array
(
[SecondArrayField1] => 2
[SecondArrayField2] => Mary
)
)
答案 0 :(得分:1)
这是您可以使用的摘录,
// first fetching key as SecondArrayField2 and SecondArrayField1 as value
$Field_3 = array_column($SecondArray, "SecondArrayField1","SecondArrayField2");
// sort by key alphabetically
ksort($Field_3);
//
$sorted = array_values(array_map(function($v) use ($ArrayToOrder) {
// first fetched Field_3 matching with current value of $Field_3 in the order
// get the index of matching Field_3
// save it to sorted array its sub array
return $ArrayToOrder[array_search($v, array_column($ArrayToOrder,'Field_3'))];
}, $Field_3));
print_r($sorted);die;
Demo。
输出:
Array
(
[0] => Array
(
[Field_1] => 13
[Field_2] => 15
[Field_3] => 3 //(Arthur)
)
[1] => Array
(
[Field_1] => 121
[Field_2] => 20
[Field_3] => 11 // (Bruce)
)
[2] => Array
(
[Field_1] => 25
[Field_2] => 17
[Field_3] => 2 // (Mary)
)
)