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>';
}
?>
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.