Home › Forums › General Issues › Using ACF With Associative Arrays
So, I’ve got this conversion chart I’m working with, and have stored its values in a simple array, wherein it lists both UK and US sizes:
$conversion = array(
"2.5" => "1",
"2.75" => "2",
"3.25" => "3",
"3.5" => "4",
"3.75" => "5",
"4" => "6"
);
I’m using Advanced Custom Fields on my WordPress site and am successfully retrieving the UK size via a Select Field. Now that’s exactly what I want, except I also want to retrieve its US value from the $conversion
array, so both values are displayed.
I’m using a Repeater field with two child Select Fields (min
and max
) and a True/False Field (to
). My code currently looks something like this:
<?php if( have_rows('repeater') ): ?>
<?php while( have_rows('repeater') ): the_row();
$min = get_sub_field('min');
$max = get_sub_field('max');
foreach($conversion as $uk => $us) {
if( get_sub_field('to') == true ) { // if ticked
if( $min == $uk ) {
echo $uk . ' ' . $us;
}
}
else {
if( $min == $uk ) {
echo $uk . ' ' . $us;
}
}
}
?>
<?php endwhile; ?>
<?php endif; ?>
Now I need a way when checking my True/False Field for the page to echo both the UK values ($min
and $max
) and the US values ($min
and $max
). Right now it’s only echoing the $min
value for the UK and the US.
I’d appreciate any help on how to achieve this. Thanks in advance!
Hi @realph
Looking at your code, you only have logic for $min within the $conversions loop. Could this be why there is no $max output?
@elliot Yeah, that’s the problem. Can’t work out how to retrieve $min/$max from $uk and $us separately. I’m so close, will bundling them both into a variable help?
Hi @realph
Perhaps you can simplify your code down to:
<?php if( have_rows('repeater') ): ?>
<?php while( have_rows('repeater') ): the_row();
$min = get_sub_field('min');
$max = get_sub_field('max');
echo 'Min = ' . $min . '(UK) ' . $conversion[ $min ] . '(US)';
echo 'Max = ' . $max . '(UK) ' . $conversion[ $max ] . '(US)';
?>
<?php endwhile; ?>
<?php endif; ?>
@elliot Simple is always better! Thanks once again, Elliot!
I had to throw in a conditional argument, though, as the ‘max’ value isn’t always present.
if( get_sub_field('to') == true ) {
echo '<p>Min: ' . $min . 'mm (US' . $conversion[ $min ] . ') – ' . $max . 'mm (US' . $conversion[ $max ] . ')</p>'; }
else {
echo '<p>Min-Max: ' . $min . 'mm (US' . $conversion[ $min ] . ')</p>'; }
One question I had is how are you pulling the $min variable in $conversion[ $min ]
? Or is that simply just grabbing the value from my sub field, matching it in the array and pumping out its associated value?
Hi @realph
You can see in my code, that $min is a variable containing the sub field value of ‘min’. Then, I use this to find the associated conversion value.
Thanks
E
The topic ‘Using ACF With Associative Arrays’ 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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.