php - 检查一个数组中某个键的值是否等于另一个数组中不同键的值

标签 php arrays multidimensional-array matching

我有 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/

相关文章:

计算给定数组中的偶数和字符

c# - new object[] {} 与 Array.Empty<object>()

c - 2维数组并打印出列和行的平均值

JavaScript 将多维数组转换为一维关联数组

php - 将 request_contact 用于 KeyboardButton 电报机器人

PHP password_verify() 与 Python bcrypt.hashpw()

php - WebMatrix 3 不显示 MySQL 数据库

php - 存储个人资料图片。 (数据库还是文件系统?)

c - 在 C 中,有没有一种方法可以使用单个格式化的打印语句打印未知大小的数组?

arrays - 将链表数据获取到 C 中的 String 数组