php - Merge arrays so they contain only recurring values -


we upgrading webshop filtering little bit different. product id's related 1 or more selected filter values.

//filter value '8 gb' $result[] = array(1,6,5,8,9);  //filter value 'amd e' (and)or 'intel' $result[] = array(1,5,8,9,10,500,502,503,600,607,608,...); 

the 'amd e' , 'intel' values same filter 'processor' these combined visitor have products amd e or intel processor.

now select id's occur in both array's. we've tried bunch of methods doesn't return expect in atempt.

the problem number of key => array pairs in $result dynamic number of id's returned sql. when first array in $result short list of id's, array_intersect() not return expected results when there multiple array's in $result.

merge_array() combine everything. visitor see products have 8 gb memory or contain amd e or intel processor.

we looking ('8 gb') , ('adm e' or 'intel') solution.

things complicated when more filters activated: ('8 gb' or '12 gb') , ('adm e' or 'intel') , ('hp' or 'apple' or 'sony')

(hope didn't loose trying explain situation , trying :s)

we've tried getting stuff done through sql. can read in this question without luck.

anyone tackled before?

as understand want have 2-step process:

  1. find items match criteria in given filter group
  2. find items returned in used filter groups

for first step you'll need find products match each of selected option. e.g. if user searches products have of options a, b or c, put results in array like:

$results = [  [1,2,3], // products have option  [2,5,7], // products have option b,  [2,7,10] // products have option c ]; 

you can ids of products have of options using

$ids = call_user_func_array('array_merge', $results); 

this give sum of arrays.

you have each filter group user has selected , you'll array of arrays on need intersection operation:

$result = [   $ids1, // array of ids match option filter group 1   $ids2  // array of ids match option filter group 2 ]; 

you can intersection calling:

$ids = call_user_func_array('array_intersect', $results); 

this give ids of products match filter option in selected filter groups.

the last thing you'll need check if user has selected filters. if not, not apply above logic return products.

i hope helps :)


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -