Get the value of a field from my blueprint and split it comma separated

Hi there,

I am fetching some values from my blueprint and it works really well. I’m trying to split the output with ->split(’,’) but since it is an string I think I have to explode/implode it? Not sure how to do that? Can anybody help me? I want to make it comma separated.

<?php
$field = $page->blueprint()->field('category');
$categories = $page->category()->split(','); 
?>  

<?php foreach($categories as $category) : ?>
    <?= $field['options'][$category] ?? html($category) ?>
<?php endforeach; ?> 

Thanks!

I guess what you want is get the labels for the field values and echo the results comma separated? Then you can do it like this:

$field = $page->blueprint()->field('category');
$categories = $page->category()->split(','); 
$categories = array_map( function(&$item) use($field) {
  $item = $field['options'][$item] ?? html($item);
  return $item;
}, $categories );
echo implode(', ', $categories);
1 Like

That’s exactly what I wanted. Much more complex than I had made out myself.
Thank you so much!