php - 匹配两个数组以从第二个数组中获取值

标签 php arrays

我有两个数组。

$queue_ids$subscriber_results

$queue_ids 输出:

Array ( [0] => 3 [1] => 4 [2] => 5 ) 

$subscriber_results 输出:

Array ( [0] => Array ( [id] => 3 [email] => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a7d3c2d4d396e7d3c2d4d389c4c8ca" rel="noreferrer noopener nofollow">[email protected]</a> [subscribed] => 1436264818 ) [1] => Array ( [id] => 4 [email] => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c7b3a2b4b3f587b3a2b4b3e9a4a8aa" rel="noreferrer noopener nofollow">[email protected]</a> [subscribed] => 1436265909 ) [2] => Array ( [id] => 5 [email] => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="80f4e5f3f4b3c0f4e5f3f4aee3efed" rel="noreferrer noopener nofollow">[email protected]</a> [subscribed] => 1436265919 ) ) 

我需要匹配这两个数组,以从 $subscriber_results 数组中获取 $queue_ids 数组中这些键的 email 记录.

我尝试使用 array_keys() 但无法让它工作。

有人能告诉我如何做到这一点吗?

最佳答案

您可以使用array_column提取一个数组,其中 id 是数组索引(键),电子邮件是值。然后compare it到队列 ID 列表 flipped所以这些值就是索引(键)。

$matchingEmails = array_intersect_key(array_column($subscriber_results, "email", "id"), array_flip($queue_ids));

关于php - 匹配两个数组以从第二个数组中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31269643/

相关文章:

java - public static int getCoins(int[][] map, int row, int col) 方法

php - MySQL:如何返回一个表中的所有行并计算另一个表中具有匹配 ID 的行数

php - 如何在 laravel 中设置可下载文件的文件名?

javascript - PHP 表单不向 MySQL 发送新数据

php - php 函数参数可以按照声明中定义的不同顺序传递吗

java - 在大文件中查找重复项和排列

c# - 创建 Array.ReturnLength() 扩展方法?

c - 可以在数组开始之前向后迭代到一个吗

java - 如何找到从唯一数组索引提取的数组中数字的最大可能总和

php - Yii 关系修改值