0

so i have multiple posts. two categories. some posts made by an author are only in one category. but other author posts might be in both categories. i need to remove duplicates. i can only have one author post at a time. so i was just going to default to the more important (shop post and not service post) category of the two and list that post.

but im puzzled on how to go about doing so.

Here is how I obtained my data and process it for output:

        $allPosts = get_post_type_data('archive', 'all');   
        for($i = 0; $i < count($allPosts); $i++){
            if($allPosts[$i]['post_type'] == 'antique-shops'){
                $shopData[] = $allPosts[$i];
            }
            if($allPosts[$i]['post_type'] == 'member-dealers'){
                $memberData[] = $allPosts[$i];
            }
            if($allPosts[$i]['post_type'] == 'services'){
                $serviceData[] = $allPosts[$i];
            }
        }
        $business = array_merge((array)$shopData, (array)$serviceData);
        function cmp($a, $b){
            return strcmp(rewrite_name($a['custom']['wpcf-business-contact'][0]), rewrite_name($b['custom']['wpcf-business-contact'][0]));
        }
        usort($business, "cmp");
        for($i = 0; $i < count($business); $i++){
            if($i % 2){
                $className = 'odd';
            } else{
                $className = 'even';
            }
            $check = check_member($business[$i]['custom']['wpcf-business-contact'][0], $memberData);
            if($check[0] == 'true'){
                echo display_listing('member-dealers', $check[1], $business[$i], $className);
            }
        }

So after getting all the data, I merge Antique Shops and Services together in one array and leave members to have their own data array.

Then for each of the business data items, i check the business contact to see if it matches with the member data.

$check returns array values. $check[0] = true/false $check[1] = array of data pertaining to the particular member.

display_listing() just takes the data entered per business/member and outputs it.

I'm thinking I need to check for duplicates right before the business or service is checked against the members data.

Each antique shop or service has a business contact: $business[$i]['custom']['wpcf-business-contact'][0] which is used to check if that business contact exists as a member.

so if $business[$i]['custom']['wpcf-business-contact'][0] belongs to both a antique shop AND a service, remove the service data pertaining to that person from the array.

I have tried this (inside the loop):

if(($key = array_search($business[$i]['custom']['wpcf-business-contact'][0], $business)) !== false){
    unset($business[$key]);
}

Any useful guidance on removing duplicates in this scenario?

hakre
  • 193,403
  • 52
  • 435
  • 836
Michael Ecklund
  • 1,206
  • 2
  • 17
  • 30
  • Maybe I didn't comprehend your question fully, but why can't you use [Array_Unique()](http://php.net/manual/en/function.array-unique.php) ? – Blake Apr 04 '12 at 15:42
  • @Blake array_unique($business); does absolutely nothing. I'm thinking we need to iterate through all of the business contacts (`$business[$i]['custom']['wpcf-business-contact'][0]`) and check if the same name occurs more than once. If it does, then use the data related the the antique shop and not the service provided by that member name. – Michael Ecklund Apr 04 '12 at 15:49
  • Michael, `Note: Note that array_unique() is not intended to work on multi dimensional arrays.` Taken from the php docs. You would need to `array_walk` a multi-dimensional array in conjunction with `array_unique`. – Blake Apr 04 '12 at 16:01
  • Michael, did you search SO for my suggestion at all? 2 seconds of searching I found this: http://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php – Blake Apr 04 '12 at 16:25
  • @Blake I figured out a solution without your snappy comment. Will post in 5 hours. Due to reputation level limits. – Michael Ecklund Apr 04 '12 at 18:21
  • Michael, I don't want to re-invent the wheel every time I try to help someone. Use suggestions to search SO or google it. It's nothing to do with snappy. If you get help and don't want to match any effort provided from others, SO isn't the place for you. – Blake Apr 04 '12 at 18:23

1 Answers1

0

I added a function called check_contact() which takes each contact name from the $serviceData array and checks it against all the $shopData array contact names. If it finds a match, it returns true.

All true matches are removed from the array before being merged into the one $business array for later usage.

I placed the following code, right before the merging of $shopData and $serviceData:

        function check_contact($name, $data){
            for($i = 0; $i < count($data); $i++){
                if($name == $data[$i]['custom']['wpcf-business-contact'][0]){
                    $return = 'true';
                }
            }
            return $return;
        }
        for($i = 0; $i < count($serviceData); $i++){
            $contact = check_contact($serviceData[$i]['custom']['wpcf-business-contact'][0], $shopData);
            if($contact == 'true'){
                unset($serviceData[$i]);
            }
        }
Michael Ecklund
  • 1,206
  • 2
  • 17
  • 30