Home › Forums › General Issues › strip out duplicate content from an array
I hope someone can help me out. I am looking for a way to strip out duplicate content from an array.
I have a post_type called catalogue, post_type called model and a post_type quality.
Within the post_type model i have a relationship field pointing to the quality and a relationship pointing to the catalogue.(post object) (the model belongs to a catalogue and is made from a certain quality)
So in the example below i am getting all the qualities that are being used in the models that have a certain catalogue number.
I am setting up an isotope filter so need to add them to a list. This works fine except it pics up duplicate qualities along the way. SO if i have 3 models that use the same qualite, the quality is added 3 times. Is there anyway to strip out duplicates. I can’t figure it out.
I hope you can help me. Thank you this is a great plugin by the way!!
<?php
function get_qualite_from_cata() {
$modeles = get_posts(array(
'post_type' => 'modele',
'meta_query' => array(
array(
'key' => 'catalogue', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
));
if( $modeles ):
foreach( $modeles as $modele ):
$qualite = get_field( 'qualite', $modele->ID);
?>
<?php if( $qualite ): ?>
<?php foreach( $qualite as $qualite ):
echo '<dd class="filtr" data-filter=".'. $qualite->ID .'">'. get_the_title( $qualite->ID ) .'</dd>';
?><?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endif;
}
// end function
?>
}
// end function
?>
To make it clearer this
<?php if( $qualite ): ?>
<?php foreach( $posts as $post ):
echo '<dd class="filtr" data-filter=".'. $post->ID .'">'. get_the_title( $post->ID ) .'</dd>';
?><?php endforeach; ?>
<?php endif; ?
>
is giving me multiple copies of the same. How can i strip the duplicates out? I have been looking at array_unique and DISTINCT but I can’t figure out how to implement this here.
I have currently implemented some jquery that removes the duplicates but i am not exactly happy about that solution. Any pointers. Thanks
Is $qualite
a regular array or an associative array?
One idea would to be to store the value of each $qualite
in a variable and check the new one against it before outputting it. Example:
$qualite = get_field( 'qualite', $modele->ID);
if( $qualite ):
$unique = '';
foreach( $qualite as $qualite ):
if ( $unique != $qualite ) :
echo '<dd class="filtr" data-filter=".'. $qualite->ID . '">'.get_the_title( $qualite->ID ) .'</dd>';
$unique = $qualite;
else : endif;
endforeach;
endif;
Daron thanks i haven’t had time yet to check this out. but i appreciate your help and will let you know how it goes.
The topic ‘strip out duplicate content from an array’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.