gpt4 book ai didi

php - 匹配php中两个数组的id

转载 作者:行者123 更新时间:2023-12-03 19:43:29 25 4
gpt4 key购买 nike

我有 2 个数组 $dizi1 和 $dizi2。这些都是这样的。

$dizi1 = [
[
'id' => 1,
'name' => 'özkan',
'surname' => 'özdemir',
],
[
'id' => 2,
'name' => 'çağrı',
'surname' => 'uğurel',
],
[
'id' => 3,
'name' => 'can',
'surname' => 'tokay'
],
[
'id' => 4,
'name' => 'lütfü',
'surname' => 'uzun'
]
];

$dizi2 = [
[
'id' => 2,
'birthday' => 1993
],
[
'id' => 3,
'birthday' => 1990
],
[
'id' => 4,
'birthday' => 1989
],
[
'id' => 1,
'birthday' => 1987
]
];

这就是我想要的

istenenDizi = [
[
'id' => 1,
'name' => 'özkan',
'surname' => 'özdemir',
'birthday' => 1987,
]
];

我研究了很多,但找不到一种算法来做到这一点。我还将创建两个 Excel 表,我将使用它。你能帮我吗我该怎么做?谢谢!

最佳答案

您可以使用foreach()array_search()array_column(),如下所示:-

$istenenDizi = [];

foreach($dizi1 as $dizi1){

$istenenDizi[$dizi1['id']] = $dizi1;
$istenenDizi[$dizi1['id']]['birthday'] = $dizi2[array_search($dizi1['id'],array_column($dizi2,'id'))]['birthday'];
}
$istenenDizi = array_values($istenenDizi);

print_r($istenenDizi);

输出:- https://eval.in/1000838

关于php - 匹配php中两个数组的id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50228744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com