dongxueji2838 2018-08-09 15:12
浏览 48
已采纳

php:array_uintersect,带有动态数组

I have a foreach loop that returns a variable number of arrays.

I need to run array_uintersect between them with a custom function to find common elements, but array_uintersect only accepts 3 parameters (2 arrays and the function).

This is what I have right now:

$all_freaktag_favorites = array();

foreach ($serie_preferite as $serie_preferita) {
    $preferite_elements = pods('category', $serie_preferita['term_id']);
    $freaktag_preferita = $preferite_elements->field('freaktag');

    $all_freaktag_favorites[] = $freaktag_preferita;
}

function comparaFreaktag($tag1, $tag2) {
    return strcmp($tag1['term_id'], $tag2['term_id']);
};

$common_freaktag_between_favorites = array_uintersect(
    $all_freaktag_favorites[0],
    $all_freaktag_favorites[1],
    'comparaFreaktag'
);

This correctly returns me the intersection between the first 2 arrays in the loop, but I don't know how to do that with an undefined number of arrays.

Thanks!

  • 写回答

1条回答 默认 最新

  • doushan6692 2018-08-09 15:14
    关注

    Put all arrays into a parent array and use the spread operator:

    $all = [[/** xxx */, [/** xxx */], [/** xxx */], ...];
    
    $all[] = 'comparaFreaktag';
    
    $common_freaktag_between_favorites = array_uintersect(...$all);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错
  • ¥15 java python或者任何一种编程语言复刻一个网页
  • ¥20 如何通过代码传输视频到亚马逊平台
  • ¥15 php查询mysql数据库并显示至下拉列表中
  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信