Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

huzzahbeard's avatar

Trying to access array offset on value of type float

I am unsure what I am doing wrong.

ErrorException Trying to access array offset on value of type float (View: D:\dev\aaron-2020\qbo-processor\resources\views\report.blade.php)

@section('content')

<div class="container">

  @if (session('status'))

      <div class="alert alert-success" role="alert">

          {{ session('status') }}

      </div>

  @endif

    <div class="row justify-content-center">

        <div class="col-md-12">

          @foreach ($report as $employeeName => $empReport)

            {{ $employeeName }}

            <ul>

              @foreach ($empReport as $tag => $data)

                <li>

                  <ul>

                      <li>{{$tag}}</li>

 

                      <li>

                        Percentage: {{ $data['percentage'] }}

                      </li>

                  </ul>

                </li>

              @endforeach

 

            </ul>

          @endforeach

        </div>

    </div>

</div>

@endsection

I can confirm there is a float in that those variables.

0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

I guess the issues is here$data['percentage'] - it is the only obvious array access ; how do you know that $data is an array?

You can us the data_get helper method to mitigate these type of issue (where a key may not exist), and it wil handle the case where $data could be something unexpected, e.g.

Percentage: {{ data_get($data, 'percentage', 'unknown') }}

This is an example, but might help... you attempt to get percentage from $data (which might be an array or object), but falls back to 'unknown' (or anything you choose) as a default.

huzzahbeard's avatar

The issue was that the last item in the array did not conform to what was expected.

Please or to participate in this conversation.