skip to Main Content

I am reading a string from a database that is composed of commas separated items,eg

3.99,10 weeks,300

That is not the problem. So I explode this:

$fn = array(explode(",",$im->features));

to get the individual items for the array.
But when I loop through it

@foreach($fn as $fun)
    {{$fun}}<br>
@endforeach

(The {{}} and @ are because this is Laravel) I get the following error:

htmlspecialchars(): Argument #1 ($string) must be of type string, array given

Any help gratefully appreciated

2

Answers


  1. Chosen as BEST ANSWER

    I have solved it. Silly mistake. I put

    $fn = array(explode(",",$im->features));
    

    instead of

    $fn = explode(",",$im->features);
    

  2. You can try the following:

    $fn = explode(",", $im->features);
    
    @foreach($fn as $fun)
        {{ $fun }}<br>
    @endforeach
    

    Note that if you want to display specific parts of each item, you will need to explode each individual item again (not sure if you need this). But you can achieve it as follows:

    $fn = explode(",", $im->features);
    
    $fnArray = [];
    foreach ($fn as $item) {
        $itemParts = explode(",", $item);
        $fnArray[] = [
            'part1' => $itemParts[0],
            'part2' => $itemParts[1],
            'part3' => $itemParts[2]
        ];
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search