简体   繁体   English

AS3或PHP-如何使用另一个数组中的多个值过滤一个数组?

[英]AS3 or PHP - How to filter an array with multiple values from another array?

Can anyone please help me with this. 谁能帮我这个忙。 Imagine you have an Indexed array that lists wines and each wine has a name, manufacture, country of origin, price, and a year. 想象一下,您有一个列出葡萄酒的索引数组,每个葡萄酒都有一个名称,制造,原产国,价格和年份。

var wines:Array = new Array(); 
wines = [ 

{winename:"Baron des Lougres", country:"France", year:"2008", price:"20.00", manufacture:"Le Yeats"},

{winename:"Barefoot Sauvignon", country:"USA", year:"2012", price:"12.00", manufacture:"Big Vines"},

]

Obviously with more wines in the indexed array/database. 显然在索引数组/数据库中有更多的葡萄酒。

I would like users to be able to filer, for example, a manufacture, year and then select a number of countries (Chili, Argentina, USA). 我希望用户能够填写例如制造年份,然后选择多个国家(智利,阿根廷,美国)。 I would have to filter the wines array with an array of selected countries. 我将不得不用一系列选定的国家来筛选葡萄酒。 I know how to sort the main array with set values using && in the if filtering but I don't know how to use it with dynamic values. 我知道如何在if过滤中使用&&对带有设置值的主数组进行排序,但是我不知道如何对动态值使用它。 The country sort could have 1 to 10 or more values, it's length would be dynamic. 国家/地区排序可以有1到10个或更多值,它的长度是动态的。 I want to have over 2000 wines in the array/database, so I would like a nice neat code CPU lite as possible. 我想在数组/数据库中拥有2000多种葡萄酒,所以我想要一个精巧的简洁代码CPU lite。

The values returned would only be from the countries selected. 返回的值仅来自所选国家/地区。 Is it possible to write an IF statement with an array as a condition or do I have to do it another way? 是否可以写一个以数组为条件的IF语句,还是我必须用另一种方式来做?

Something like: 就像是:

for (var i:uint = 0; i < wines.length; i++) 
    if (wines.year == selected_year 
&& wines.manufacture == selected_manufacture
&& wines[i].country == selected_countries_array)
{
    trace(wines[i].name);
    }
}

UPDATE 更新

The answer below, from bwroga worked. 下面的答案来自bwroga。 It looks a bit different from the code above but that is only because I edited this question after he answered it. 它看起来与上面的代码有些不同,但这仅仅是因为我在他回答之后编辑了这个问题。 But his code, the indexOf part works perfect. 但是他的代码,indexOf部分工作完美。 Thank you bwroga. 谢谢bwroga。 I tried the following and it worked! 我尝试了以下方法,它起作用了! :) :)

for (var i:uint = 0; i < wines.length; i++) 
    if (wines.year == selected_year 
&& wines.manufacture == selected_manufacture
&& selected_countries_array.indexOf(wines[i].country != -1)
{
    trace(wines[i].name);
    }
}

You can do this: 你可以这样做:

// Actionscript
if(year == selected_year && 
   manufacture == selected_manufacture && 
   selected_counties_array.indexOf(country) !== -1
){
//action
}

If the country is in selected_countries_array, indexOf would return the index of the country in the array. 如果国家/地区位于selected_countries_array中,则indexOf将返回该国家/地区在数组中的索引。 If it is not in the array, it returns -1. 如果它不在数组中,则返回-1。

In PHP, you would do this: 在PHP中,您可以这样做:

// Php
if($year == $selected_year && 
   $manufacture == $selected_manufacture && 
   in_array($country, $selected_counties)
){
//action
}

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

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