简体   繁体   English

PHP中的连续数组交集

[英]Consecutive array intersections in php

I am writing a file meta data app in php, and I need to search for files which have several tags. 我正在用php编写文件元数据应用程序,我需要搜索具有多个标签的文件。 The information is stored in a mySQL table with a fileId and a tagID. 该信息与fileId和tagID存储在mySQL表中。 The way I do this is by searching the table for the first tagId and then take the intersection of the resulting file list with consecutive SQL calls for the remaining tags: 我这样做的方法是在表中搜索第一个tagId,然后将结果文件列表与其余标签的连续SQL调用相交:

$files = $cfiles->searchFiles($owner,$tags[0]);                                                                                     
foreach($tags as $tag){                                                           
  $temp = $cfiles->searchFiles($owner, $tag);
  $files = array_intersect($files, $temp);
}    

The problem is that array_intersect creates an associative array, ie, 问题是array_intersect创建了一个关联数组,即

$files= array("id1","id2",..."idn"); $ files = array(“ id1”,“ id2”,...“ idn”);

becomes 变成

$files = array("0" => "id1", "1" => "id2", ... $ files = array(“ 0” =>“ id1”,“ 1” =>“ id2”,...

when I take the intersection with itself. 当我与自己相交时。 The problem is, that on the next iteration for the next tag, array_intersection fails because I then take the intersection between an associative array and a non-associative array. 问题在于,在下一个标记的下一次迭代中,array_intersection失败,因为然后我将关联数组与非关联数组进行交集。 Can somebody please enlighten me? 有人可以启发我吗?

So it turns out that array_intersect does not work when the entries of the arrays are arrays themselves (as they are in my case). 因此事实证明,当数组的条目是数组本身时,array_intersect不起作用(就像我的情况一样)。 Using array_uintersect() does the trick. 使用array_uintersect()可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM