Create a list with commas from an array in laravel / blade?

I show the elements of an array @foreach($tags as $tag)$tag->@endforeach. Output signal tag1tag2tag3. What is the possible way to sho array elements in tag1,tag2,tag3. And how not to show ,if there is only one element in the array.

+4
source share
5 answers

implode()good for repeating simple data. In a real project, you usually want to add some HTML or logic to the loop, use the variable $loopavailable from 5.3:

@foreach ($arrayOrCollection as $value)
    {{ $loop->first ? '' : ', ' }}
    <span class="nice">{{ $value->first_name }}</span>
@endforeach
+6
source

Use implode:

{{ implode(', ', $tags) }}
+2
source

implode - , ,

{{ join(', ', $tags) }} 

.

0

implode():

$arr = ['one', 'two', 'three'];
echo implode(',', $arr);

//

one,two,three
0

I believe that what you are looking for could be something like this: // have your array in php tags // $ arr = ['one', 'two', 'three'] ;? > // go through the array with foreach and if the counter of the array is not equal to the las element, then put coma after it

@foreach ($arr as $key => $value)
    @if( count( $arr ) != $key + 1 )
        {{ $value }},
     @else
        {{ $value }}
    @endif
@endforeach
0
source

Source: https://habr.com/ru/post/1661171/


All Articles