我有数组,例如:
$arr = [
0=>['note_id'=>1,'content'=>1],
1=>['note_id'=>2,'content'=>2],
2=>['note_id'=>3,'content'=>3],
];
我有一组 id:
$ids=[2,3,1];
我需要通过使用按值 'note_id' 和数组ids对该数组进行排序来从arr获取新数组,因此结果必须是:
$arr = [
1=>['note_id'=>2,'content'=>2],
2=>['note_id'=>3,'content'=>3],
0=>['note_id'=>1,'content'=>1],
];
有没有这方面的功能?谢谢!
您可以循环这两个数组并将$arr
with的键与 的值进行比较$ids
并创建新数组$newArr
。
$arr = [
0 => ['note_id' => 1, 'content' => 1],
1 => ['note_id' => 2, 'content' => 2],
2 => ['note_id' => 3, 'content' => 3],
];
$ids = [2, 3, 1];
$newArr = [];
foreach ($ids as $id) {
foreach ($arr as $keyArr => $item) {
if ($id === $item['note_id']) {
$newArr[$keyArr] = $item;
}
}
}
print_r($newArr);
结果:
Array
(
[1] => Array
(
[note_id] => 2
[content] => 2
)
[2] => Array
(
[note_id] => 3
[content] => 3
)
[0] => Array
(
[note_id] => 1
[content] => 1
)
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句