Home › Forums › General Issues › multiselect how to output value and label
Hello,
I have a multiselect that I need the output the values in one place and the corresponding labels in another place in the page, but I know very little of php and have no idea how to create the foreach to get only the value in one loop and the labels in another loop.
Can someone help me? My multiselect is configured to output label and value in array.
Thank you very much!
This is more or less my code:
<?php $especie_de_acao_array = get_field( ‘especie_de_acao’ ); ?>
// here I need the label…
<?php
$i = 0;
$len = count($especie_de_acao_array);
foreach ($especie_de_acao_array as $item) {
if ($i == 0) {
echo ““; echo $item; echo ““; echo ” c/c “; //first
} elseif ($i == $len – 1) {
echo ““; echo $item; echo ““; echo “,”; //last
} else {
echo ““; echo $item; echo ““; echo “, c/c “;
}
$i++;
}
?>
.
.
.
.
//here I need to output the corresponding value:
<?php
$i = 0;
$len = count($especie_de_acao_array);
foreach ($especie_de_acao_array as $item) {
if ($i == 0) {
echo ““; echo $item; echo ““; echo ” c/c “; //first
} elseif ($i == $len – 1) {
echo ““; echo $item; echo ““; echo “,”; //last
} else {
echo ““; echo $item; echo ““; echo “, c/c “;
}
$i++;
}
?>
I was able to figure it out. If someone also need:
$labels = array();
$field = get_field_object( ‘especie_de_acao’ );
$values = get_field(‘especie_de_acao’);
foreach ($values as $value) {
$labels[] = $field[‘choices’][ $value ];
}
$i = 0;
$len = count($labels);
if ($len == 0) { echo ‘<strong style=”color: red;” >’; echo “(especifique um tipo de ação)”; echo ““;}
elseif ($len == 1) {
echo ““; echo $labels[0]; echo ““;
} else {
foreach ($labels as $label) {
if ($i == 0) {
echo ““; echo $label; echo ““; echo ” c/c “; //first
} elseif ($i == $len – 1) {
echo ““; echo $label; echo ““; echo “,”; //last
} else {
echo ““; echo $label; echo ““; echo “, c/c “;
}
$i++;
}
}
if ($len == 0) { echo “(especifique um tipo de ação)”;}
elseif ($len == 1) {
echo $values[0];
} else {
foreach ($values as $value) {
if ($i == 0) {
echo $value; echo “, “; //first
} elseif ($i == $len – 1) {
echo ” e “; echo $value; echo “,”; //last
} else {
echo $value; echo “, “;
}
$i++;
}
}
The topic ‘multiselect how to output value and label’ 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.