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 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程