11

I need to check if the data array has a specific key, I tried it like this:

@if ( ! empty($data['currentOffset']) )
    <p>Current Offset: {{ $currentOffset }} </p>
@else
    <p>The key `currentOffset` is not in the data array</p>
@endif

But I always get <p>The keycurrentOffsetis not in the data array</p>.

4 Answers 4

28

You can use @isset:

@isset($data['currentOffset'])
    {{-- currentOffset exists --}}
@endisset
Sign up to request clarification or add additional context in comments.

2 Comments

is there also something like elseisset? I need an else branch.
@Black no. If you need else, you could just use @if (isset($data['currentOffset'])) or the ternary operator {{ isset($data['currentOffset']) ? 'Exists' : 'Doesn\'t exist' }}
5

I think you need something like this:

 @if ( isset($data[$currentOffset]) )
 ...

2 Comments

Thats exactly what @Alexey Mezenin posted 4 min ago.
no there is difference between $data[$currentOffset] and $data['currentOffset'], I think maybe the key was stored in a variable $currentOffset
4

Use following:

@if (array_key_exists('currentOffset', $data))
    <p>Current Offset: {{ $data['currentOffset'] }} </p>
@else
    <p>The key `currentOffset` is not in the data array</p>
@endif

Comments

0

ternary

 @php ($currentOffset = isset($data['currentOffset']) ? $data['currentOffset'] : '')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.