带有未知键数的多维数组上的PHP Array_intersect [英] PHP Array_intersect on multidimensional array with unknown number of keys

查看:77
本文介绍了带有未知键数的多维数组上的PHP Array_intersect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在包含资源(人员)的应用程序中制作高级搜索过滤器。我将所有结果放在1个多维数组中。该应用程序的用户可以搜索人员的职位,技能,工作领域和国家。

I'm trying to make advanced search filters in an application that holds resources (people). I've got all the results in 1 multidimensional array. A user of the application can search for the persons Job title, skills, work field and country.

我已经完成了查找相识人员的部分用户给出的标准。这些结果存储在多维数组中。如果用户正在寻找具有职位名称和特定技能的特定资源的人,则返回值为:

I've already made the part where I look up the people that meet the criteria given by the user. These results are stored in a multidimensional array. If the user is looking for someone with a specific resource with a job title and a specific skill the return value is this:

$realfilters = array(2) {
["resourcesWithJobtitle"]=> array(6) {
   [0]=> string(1) "1"
   [1]=> string(2) "48"
   [2]=> string(2) "88"
}
["resourcesWithSkill"]=> array(9) {
   [0]=> string(1) "4"
   [1]=> string(1) "8"
   [2]=> string(1) "48"
   [3]=> string(2) "50"
}

当用户还查找工作字段时这会添加到结果中:

When the user also looks for a work field this is added to the result:

["resourcesWithWorkfield"]=> array(3) {
   [0]=> string(2) "48"
   [1]=> string(2) "96"
   [2]=> string(2) "97"
}

我需要知道哪些资源满足所有维度数组,以便我可以显示它们。 (因此,在此示例中,我需要一个仅包含1个值的数组:48)。我想我需要使用 array_intersect ,但似乎无法正确使用。

I need to know which resources meet all dimensions of the array so I can display them. (So in this example I need an array with just 1 value: 48). I think I need to use array_intersect but can't seem to get it right.

推荐答案

可能的解决方案之一:您可以先 extract () $ realfilters 数组值应用于变量,然后将 array_intersect()给他们。但是仅当可能的过滤器不多时,此解决方案才适用。

One of the possible solutions: you may first extract() the $realfilters array values to variables, and then apply the array_intersect() to them. But this solution is applicable only if there are not many possible filters.

另一个可能也是最好的解决方案是循环相交,例如:

Another one and probably the best solution would be to intersect in a loop, something like:

$res_arr = array_shift($realfilters);
foreach($realfilters as $filter){
     $res_arr = array_intersect($res_arr, $filter);
}

这篇关于带有未知键数的多维数组上的PHP Array_intersect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆