Support

Account

Home Forums Add-ons Repeater Field Echo same value just once

Helping

Echo same value just once

  • Hi!

    I’m having a little problem that I think should be solved easily with some kind of unique array or foreach function.

    My code looks like this:

    <? if( get_field('kontaktpersoner') ): ?>
    		<?php while( has_sub_field('kontaktpersoner') ): ?>
            
    		<?php $arbetsgrupp = get_sub_field('arbetsgrupp'); ?>
            
            <?php echo $arbetsgrupp; ?>
            
            <?php endwhile; ?>
    		<?php endif;  ?>

    The $arbetsgrupp can be “Red”, “Green” and “Blue”.

    My problem is that if there is more than one “Red” it’s displayed 2 times or more but I want all values to be uniqe. Just one output for each value.

    Am I making any sence?

    Instead of echoing Red, Red, Blue, Green, Blue I want it to be: Red, Blue, Green.

  • Hello Henric,

    This is untested but this should do it:

    
    <?php
    if( get_field('kontaktpersoner') )
    {
    	$arbetsgruppen = array();
    	while( has_sub_field('kontaktpersoner') )
    	{
    		$arbetsgrupp = get_sub_field('arbetsgrupp');
    		if(!in_array($arbetsgrupp,$arbetsgruppen))
    		{
    			array_push($arbetsgruppen,$arbetsgrupp);
    		}
    	}
    	foreach($arbetsgruppen as $arbetsgrupp)
    	{
    		echo $arbetsgrupp;
    	}
    }
    ?>
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.