-1

I am looking for a way to strip out duplicate content from an array. The problem is that these are essentially all different arrays obtained from while loop

I have 2 custom post types "Schools" and "Courses" that have an ACF field of relationships between each other. On the course taxonomy pages, I display the courses of the current taxonomy, as well as schools related to the courses of this taxonomy. Thus, schools are repeated as many times as there are courses associated with that school.

So, this is

<?php
while (have_posts()) :
    the_post();
    $school_items = get_field('shkola');
    foreach ($school_items as $school_item) :

    endforeach; ?>
    <pre><?php print_r($school_item); ?></pre>
<?php endwhile; ?>

Gives me records containing duplicates

WP_Post Object ([ID] => 37)
WP_Post Object ([ID] => 37)
WP_Post Object ([ID] => 32)

I tried array_unique, but I couldn't use it correctly, it still comes back with duplicates.

This is very similar to the situation here https://support.advancedcustomfields.com/forums/topic/strip-out-duplicate-content-from-an-array/, but the method described there also did not work for me.

I will be grateful for any help in this matter)

UPDATE Output from print_r($school_items);

Array ([0] => WP_Post Object ( [ID] => 37 ) ) 
Array ([0] => WP_Post Object ( [ID] => 37 ) ) 
Array ([0] => WP_Post Object ( [ID] => 32 ) ) 
6
  • Are you saying that the <pre><?php print_r($school_item); ?></pre> after the loop completes give that array of 3 WP_Post Objects? Because that is not likely unless a school_item is itself an array Commented Oct 20, 2021 at 9:50
  • Yes, that's right. Maybe this is because the request for the "Schools" post type is being done on the "Courses" taxonomy page. I have taxonomies for "Courses" (categories of courses). They first have all the courses assigned to this category, and then "Schools" are needed. Commented Oct 20, 2021 at 10:56
  • Please show me a print_r($school_items); done just after the line $school_items = get_field('shkola'); line. Thanks Commented Oct 20, 2021 at 11:59
  • Thank you for your help). It looks like this: Array ([0] => WP_Post Object ( [ID] => 37 )) Array ([0] => WP_Post Object ( [ID] => 37 )) Array ([0] => WP_Post Object ( [ID] => 32 )) Commented Oct 20, 2021 at 12:25
  • Ok the code I gave you should only print the title once and ignore the duplicates, however it does not remove items from the $school_items array. Do you want to actually remove the duplicate ID's from that array as well as not print a duplicate title Commented Oct 20, 2021 at 12:32

1 Answer 1

2

You could use a simple, have I seen this before test

<?php
$school_items = get_field('shkola');
if ($school_items) :
    $seenIt = [];
    foreach ($school_items as $school_item) :
        
        if ( !in_array($school_item->ID, $seenIt) ) :
            $title = get_the_title($school_item->ID);
?>
            <div class="title">
                <h2><?php echo esc_html($title); ?></h2>
            </div>
<?php
            $seenIt[] = $school_item->ID;
        endif;
    endforeach;
endif; 
?>

Ok, so if you want to remove the duplicates from the original array as well this will also do that

$school_items = get_field('shkola');
if ($school_items) :
    $seenIt = [];
    foreach ($school_items as $key => $school_item) :
        
        if ( in_array($school_item->ID, $seenIt) ) :
            unset($school_items[$key]);
        else:
            $title = get_the_title($school_item->ID);
?>
            <div class="title">
                <h2><?php echo esc_html($title); ?></h2>
            </div>
<?php
            $seenIt[] = $school_item->ID;
        endif;
    endforeach;
endif; 
Sign up to request clarification or add additional context in comments.

9 Comments

Unfortunately, it doesn't work. Your answer is very similar to the answer from the link from my post, but this solution does not help(
In what way does it not work, thats not a very helpful description
Just no changes. Records continue to be duplicated.
Are you sure you copies the code line by line?
Yes, I just copied it as you wrote, outputs 3 entries, 2 of them are the same with id 37.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.