php - 检查一个数组中某个键的值是否等于另一个数组中不同键的值
问题描述
我有 2 个多维数组,我想获得第一个数组,其中 [file]
数组 1 中的键值等于[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;
}
}
}
推荐阅读
- javascript - 在对“图像”数组进行排序后,如何在元素之间进行转换?
- git - git difftool $BASE 和 $LOCAL 颠倒了
- angular - @ContentChildren 为什么我不能按类型查询材质实例
- android - 如何在我的应用程序中修复 IllegalArgumentException 改造
- python - 使用python将今天收到的电子邮件中的附件复制到文件夹
- c++ - ZMQ 上下文被终止(线程)
- python - 如何在python中动态返回当前时间
- javascript - svg url 不使用 parceljs 编译
- python - 无法更改 matplotlib 中的默认颜色图
- service - 无法在 PowerBI 服务中计划刷新