简体   繁体   中英

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. The country sort could have 1 to 10 or more values, it's length would be dynamic. I want to have over 2000 wines in the array/database, so I would like a nice neat code CPU lite as possible.

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?

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. 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. Thank you 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. If it is not in the array, it returns -1.

In PHP, you would do this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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