Support

Account

Home Forums Add-ons Repeater Field How to display a value in array ones if it exists in the sub-field? Reply To: How to display a value in array ones if it exists in the sub-field?

  • Hi @kidcajes

    Thanks for the info.
    You will need to loop through your rows and first create an array which holds only the different states.

    Next, loop through these states and then find all the rows which match the current state.

    Something like this should work:

    
    <?php 
    
    $states = array();
    $rows = get_field('state_lists'); 
    
    if( $rows )
    {
    	foreach( $rows as $row )
    	{
    		// vars
    		$state = $row['state_name'];
    		$link = $row['state_link'];
    		
    		
    		// add state to list of states
    		if( ! in_array($state, $states) )
    		{
    			$states[] = array(
    				'title' => $state,
    				'link' => $link,
    			)
    		}
    		
    	}
    }
    		
    		
    
    if( $states )
    {
    	echo '<ul class="state-list">';
    	
    	foreach( $states as $state )
    	{
    		echo '<li class="repeater-item one-fourth">
    				<a title="'. $state['title'] . '" href="' . $state['link'] .'">
                    	<span class="repeater-title">' . $state['title']. '</span>
                    </a>
                    <ul class="city_list">';
                        
                    	foreach( $rows as $row )
                        {
                        	// validate
                        	if( $state != $row['state_name'] )
                        	{
    	                    	continue;
                        	}
                        	
    	                    echo '<li class repeater-item one-third">
    	                          <a title="'. $row['city_name'] . '" href="' .$row['city_link'] . '">
    	                          <span class="repeater-title"> ' .$row['city_name']. ' </span>
    	                        </li>';
                        }
                            
    				echo '</ul>
                  </li>' ;
    		
    	}
    	
    	echo '</ul>';
    }
     ?>