Get some specify items from multiselect field?

For example

I have multiselect field with this varibales

{
1: One
2: Two
3: Three
4: Four
5: Five
}

In panel, I selected 1,3,5 items

So on detail page i will see them with this code

<?php foreach($page->numbers()->split() as $number): ?>
<?php echo $number; ?>
<?php endforeach; ?>

But how can i see only 1 and 5 items on listing page? Not all items, just specified…
I need this because on detail page will be about 20 selected items, but on listing page with short description I want to show only 3 items max… but only specified images (that i will premade in template)

Thanks

Define an array of the items you want:

$onlyshow = array(1, 5);

And then inside your loop:

if(in_array($number, $onlyshow)) {
  echo $number;
}
1 Like

Ok, but how can I set to $number specify html output

item 1 - <i class="fa fa-check color8"></i> 
item 2 - <i class="fa fa-check color9"></i>

etc… this will work?

<?php if($number == '1'): ?>
	<i class="fa fa-star-o color9"></i>
<?php endif ?>

Thanks! Yep It works…

For all newbies :slightly_smiling:

If you have checkboxes or multiselect field and you need to show only predefined array you need something like this

<?php foreach($numbers->number()->split() as $number): ?>
<?php $onlyshow = array(1, 3, 5); ?>
<?php if(in_array($number, $onlyshow)) { ?>
	<?php if($number == '1'): ?>
		text or image or icon... 
	<?php endif ?>
	<?php if($number == '3'): ?>
		text or image or icon... 
	<?php endif ?>
	<?php if($number == '5'): ?>
		text or image or icon... 
	<?php endif ?>
	<?php } ?>
<?php endforeach; ?>