1

I'm trying to refine a search list so that when a user clicks on checkboxes it refines the results based on the criteria selected. Here is the PHP function I'm using:

function($criteria_a,$criteria_b,$criteria_c,$criteria_d){
   if($criteria_a==1){
      // build array
      $a_array=array($user_a,$user_e,$user_f);  
   }
   if($criteria_b==1){
      // build array
      $b_array=array($user_a,$user_c,$user_e);  
   }
   if($criteria_c==1){
      // build array
      $c_array=array($user_b);  
   }
   if($criteria_d==1){
      // build array
      $d_array=array($user_a,$user_e);  
   }
   $main_array = array_merge($a_array,$b_array,$c_array,$d_array);
}

Basically, when a user clicks on the criteria A checkbox (which could be something like 'Show those under 30') it builds up $a_array with a list of the users that meet the criteria.

But the problem is this: if I specified criteria A, B and D as 1, it fills the $main_array with users who meet any of the criteria instead of users who meet all the criteria. The main array would look like this:

array($user_a,$user_e,$user_f,$user_a,$user_c,$user_e,$user_a,$user_e);

But I only want it to list the users that meet all the criteria:

array($user_a,$user_e);

These users meet each of the criteria being specified instead of any of the criteria being specified. I'm also not sure if this helps:

$acv = array_count_values($main_array);
arsort($acv);
$main_array = array_keys($acv);

Does anybody know what I can do?

3 Answers 3

1

So, you want to have an array that contains users present in all the arrays that were given? This seems like a simple case of array_intersect. Use it to look at all 4 arrays and it will create an array containing all values that appear in every array. In order for all the values not to be thrown out, you'll need to set all the arrays to a default value and then change them based on the criteria.

function($criteria_a,$criteria_b,$criteria_c,$criteria_d){
$a_array = $b_array = $c_array = $d_array = array($user_a,$user_b,$user_c,$user_d,$user_e,$user_f);
if($criteria_a==1) $a_array=array($user_a,$user_e,$user_f);  
if($criteria_b==1) $b_array=array($user_a,$user_c,$user_e);  
if($criteria_c==1) $c_array=array($user_b);  
if($criteria_d==1) $d_array=array($user_a,$user_e);  
$main_array = array_intersect($a_array,$b_array,$c_array,$d_array);
}

In this example, you're more limiting what users it could be based on the criteria, rather than setting which users it could possibly be. You also won't get any notice errors for undefined variables when attempting to use the array_intersect function.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked great. The key to this function is ensuring that empty arrays don't break the main_array when they're all intersected. Thanks
0

Just change:

array_merge($a_array,$b_array,$c_array,$d_array);

To:

array_intersect($a_array,$b_array,$c_array,$d_array);

array_intersect()

1 Comment

This would work if you dealt with the arrays that are potentially empty
0

array_merge() works by 'gluing' one array to another, but it doesn't check to make sure the values are unique.

You can solve your issue two ways:

  • Use array_merge() followed by array_unique() to remove duplicate values

  • Turn those arrays into associative arrays like this:

    if($criteria_a==1){
        // build array
        $a_array=array($user_a,$user_e,$user_f);  
    }
    

    When you use array_merge() on an associative array, there will only be one value per key. You can then grab just the values with array_values()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.