学习整理在php中一个二维数组按另一个一维数组顺序排序
- 一维数组数据
- 需要按照$a排序的二维数组
- 实现方式:
- 最终返回结果:
一维数组数据
$a = [
100,
102,
104
];
需要按照$a排序的二维数组
$b = [
104 => [
'id' => '104',
'qipa250' => '104qipa250'
],
105 => [
'id' => '105',
'qipa250' => '105qipa250'
],
102 => [
'id' => '102',
'qipa250' => '102qipa250'
],
100 => [
'id' => '100',
'qipa250' => '100qipa250'
],
101 => [
'id' => '101',
'qipa250' => '101qipa250'
]
];
实现方式:
$intersect_key = array_intersect_key(array_flip($a), $b);
$data = array_replace($intersect_key, $b);
最终返回结果:
array:5 [
100 => array:2 [
"id" => "100"
"qipa250" => "100qipa250"
]
102 => array:2 [
"id" => "102"
"qipa250" => "102qipa250"
]
104 => array:2 [
"id" => "104"
"qipa250" => "104qipa250"
]
105 => array:2 [
"id" => "105"
"qipa250" => "105qipa250"
]
101 => array:2 [
"id" => "101"
"qipa250" => "101qipa250"
]
]