我有 2 个多维数组,我想获取第一个数组,其中数组 1 中 [file]
键的值等于 [folder_name]
键的值在数组 2 中
$arr1 = [
[
'is_dir' => '1',
'file' => 'hello member',
'file_lcase' => 'hello member',
'date' => '1550733362',
'size' => '0',
'permissions' => '',
'extension' => 'dir',
],
[
'is_dir' => '1',
'file' => 'in in test',
'file_lcase' => 'in in test',
'date' => '1550730845',
'size' => '0',
'permissions' => '',
'extension' => 'dir',
]
];
$arr2 = [
[
'dic_id' => '64',
'folder_name' => 'hello member',
'share_with' => '11',
],
[
'dic_id' => '65',
'folder_name' => 'hello inside',
'share_with' => '11',
],
[
'dic_id' => '66',
'folder_name' => 'in in test',
'share_with' => '11',
],
];
我尝试过循环 2 个数组并到达一个数组,但没有成功。
最佳答案
我们可以在彼此内部迭代两个数组以进行检查,直到找到匹配为止。
请注意,这仅显示第一个匹配项。如果您想保留所有匹配项,您应该使用另一个助手 array
来存储与第二个数组匹配的第一个数组值。
foreach ($array1 as $key => $value) {
foreach ($array2 as $id => $item) {
if($value['file'] == $item['folder_name']){
// we have a match so we print out the first array element
print_r($array1[$key]);
break;
}
}
}
关于php - 检查一个数组中某个键的值是否等于另一个数组中不同键的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54804683/