Copying a lot of code to filter four PHP arrays

Posted on

Problem

I have made this search filter and it works fine, but I think the code looks a little messy and I have no idea how to clean it up.

I hope the following makes sense, otherwise please tell me which information you need, and I will provide it ASAP.

// GET THE POST VALUE FROM FORM FIELDS
          $field_value_service =  $_POST['service_filter'];
          $field_value_region  =  $_POST['service_region'];
          $field_value_postal  =  $_POST['postal_code'];
          $field_value_gender  =  $_POST['gender_filter'];

          // GET USER INFO FROM USERS WHERE USERS VALUES IS EQUAL TO FORM FIELD VALUES
          $valuesService = get_cimyFieldValue(false, 'service', $field_value_service);
          $valuesRegion  = get_cimyFieldValue(false, 'region', $field_value_region);
          $valuesPostal  = get_cimyFieldValue(false, 'postnummer', $field_value_postal);
          $valuesGender  = get_cimyFieldValue(false, 'gender', $field_value_gender);

          // CREATE EMPTY ARRAYS
          $userArray = array();
          $userArrayTwo = array();
          $userArrayThree = array();
          $userArrayFour  = array();
          $userArrayFive = array();

          foreach ($valuesService as $value) {
            $userArray[] = $value['user_id'];
          }
          foreach ($valuesRegion as $value) {
            $userArrayTwo[] = $value['user_id'];
          }
          foreach ($valuesPostal as $value) {
            $userArrayThree[] = $value['user_id'];
          }
          foreach ($valuesGender as $value) {
            $userArrayFour[] = $value['user_id'];
          }

          $firstResult = array_merge($userArray, $userArrayTwo);
          $duplicates = array_unique(array_diff_assoc($firstResult, array_unique($firstResult)));
          $secondResult = array_merge($duplicates, $userArrayThree);
          $secondDuplicates = array_unique(array_diff_assoc($secondResult, array_unique($secondResult)));
          $thirdResult = array_merge($secondDuplicates, $userArrayFour);
          $thirdDuplicates = array_unique(array_diff_assoc($thirdResult, array_unique($thirdResult)));

Solution

  • comments should explain what the code does, at least the complicated blocks, such as the array merging, and/or a comment to describe the whole thing

  • Be consistent with naming things: you use camelCase sometimes (e.g. userArray) and underscores other times (e.g. field_value_gender). IIRC, underscores is the PHP style, so you could go with that, but the important thing is to be consistent.

  • Same goes for your filter parameter names: service_filter, region_filter, postal_code_filter, gender_filter

  • When you have places where you repeat code, use a function:

    function build_array(post_field, user_field) {
        $post_value = $_POST[post_field];
        $user_value = get_cimyFieldValue(false, user_field, $post_value);
        $arr = array();
    
        foreach ($user_value as $value) {
            $arr[] = $val['user_id'];
        }
        return $arr;
    }
    
    build_array('service_filter', 'service');
    
  • But really, you probably don’t need that. Are you trying to get a unique list of user IDs? Can you use one array and call array_unique on that? Or, are you trying to get duplicates? Or both? Either way, I really doubt you need all that merge/diff/unique’ing. For example, you could keep an array of user_id => times_seen and then loop through that to get entries with times_seen > 1 or whatever.

Leave a Reply

Your email address will not be published. Required fields are marked *