Laravel Blade - 检查数据阵列是否具有特定密钥

问题描述 投票:6回答:3

我需要检查数据数组是否有特定的密钥,我试过这样:

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

但我总是得到<p>The keycurrentOffsetis not in the data array</p>

laravel laravel-blade
3个回答
9
投票

你可以使用@isset

@isset($data['currentOffset'])
    {{-- currentOffset exists --}}
@endisset

2
投票

使用以下:

@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

1
投票

我想你需要这样的东西:

 @if ( isset($data[$currentOffset]) )
 ...
© www.soinside.com 2019 - 2024. All rights reserved.