Laravel Blade - 检查数据数组是否具有特定键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48100830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:11:59  来源:igfitidea点击:

Laravel Blade - check if data array has specific key

laravellaravel-blade

提问by Black

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>.

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

回答by Alexey Mezenin

You can use @isset:

您可以使用@isset

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

回答by Sahil Purav

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

回答by YouneL

I think you need something like this:

我认为你需要这样的东西:

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

回答by DEV Tiago Fran?a

ternary

三元

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