Lixi
Lixi

Reputation: 142

Can't access data in a nested array

I am kinda stuck, so I need you help.

I have an nested array that looks like this:

    array (size=2)
      0 => 
        array (size=5)
          'id' => int 7
          'name' => string 'sad' (length=3)
          'content' => string 'x' (length=1)
          'created_at' => string '2016-03-08 17:41:12' (length=19)
          'nm' => string 'test' (length=4)
      1 => 
        array (size=5)
          'id' => int 8
          'name' => string 'sadfafs' (length=7)
          'content' => string 'x' (length=1)
          'created_at' => string '2016-03-08 17:41:44' (length=19)
          'nm' => string 'test' (length=4)

I am trying to access data inside with this method:

@if ($forms != null)
         @foreach($forms as $forma)
             @foreach($forma as $form)
            <tr class="gradeA">
                  <td>{!! $form['id'] !!}</td>
                  <td>{!! $form['name'] !!}</td>
                  <td class="center">{!! $form['created_by'] !!}</td>
                  <td class="center">{!! $form['created_at'] !!}</td>
                  <td>
                      <a href="{{url('forms/delete?id=' . $form[0])}}">DELETE</a>
                      <a href="{{url('forms/edit?id=' . $form[0])}}">EDIT</a></td>
              </tr>
             @endforeach

         @endforeach
     @else
         <p> Nufing</p>
@endif

Whatever I am doing I get error: Illegal string offset "name"

Can someone advise me on how access this data in a correct manner?

Upvotes: 1

Views: 94

Answers (1)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

You need to remove inner foreach loop and every thing works fine. So code should be:-

@if ($forms != null)
         @foreach($forms as $forma)
            <tr class="gradeA">
                  <td>{!! $form['id'] !!}</td>
                  <td>{!! $form['name'] !!}</td>
                  <td class="center">{!! $form['created_by'] !!}</td>
                  <td class="center">{!! $form['created_at'] !!}</td>
                  <td>
                      <a href="{{url('forms/delete?id=' . $form[0])}}">DELETE</a>
                      <a href="{{url('forms/edit?id=' . $form[0])}}">EDIT</a></td>
              </tr>
         @endforeach
     @else
         <p> Nufing</p>
@endif

Upvotes: 2

Related Questions