php Laravel 检查数组是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39342608/
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-08-26 01:51:59  来源:igfitidea点击:

Laravel Check if Array is empty

phplaravelforeach

提问by Mamulasa

I am struggling with displaying some content depending on if an array property does have a value or not. If an article has a title, I want to display the content of the entire article, if not I want to show something else. However, it doesn't work. The code in the else statement is not executed.

我正在努力根据数组属性是否具有值来显示一些内容。如果一篇文章有​​标题,我想显示整篇文章的内容,否则我想显示其他内容。但是,它不起作用。else 语句中的代码不会被执行。

What's wrong here?

这里有什么问题?

My Code is:

我的代码是:

@foreach($restaurantmenue as $daily)
                    @foreach($daily->articles as $menue)
                     @if(!empty($menue->title))
                       <div class="card card-horizontal">
                         <div class="row">
                           <div class="col-md-5">
                             <div class="image" style="background-image: url({{asset('images/frontend/profile/profileHero.jpg')}}); background-size: cover; background-position: center center;">
                               <img src="{{asset('images/frontend/profile/profileHero.jpg')}}" alt="..." style="display: none;">
                               <div class="filter filter-azure">
                                 <button type="button" class="btn btn-neutral btn-round">
                                     <i class="fa fa-heart"></i> SM?CKT MIR
                                 </button>
                               </div>
                             </div>
                           </div>
                           <div class="col-md-7">
                                <div class="content">
                                  <p class="category text-info">
                                       <i class="fa fa-trophy"></i> Best of
                                   </p>

                                   <a class="card-link" href="#">
                                       <h4 class="title">{{$menue->title}} </h4>
                                   </a>
                                   <a class="card-link" href="#">
                                       <p class="description">{{$menue->body}}</p>
                                       <br />
                                       {{$menue->price}} 
                                   </a>
                                    <div class="footer">
                                       <div class="stats">
                                           <a class="card-link" href="#">
                                              <i class="fa fa-male"></i> {{$restaurant->name}}
                                           </a>
                                       </div>
                                       <div class="stats">
                                         <a class="card-link" href="#">
                                           <i class="fa fa-comments"></i> 23 Comments
                                         </a>
                                       </div>
                                       <div class="stats">
                                          <a class="card-link" href="#">
                                           <i class="fa fa-heart"></i> 231 Likes
                                          </a>
                                       </div>
                                   </div>
                               </div>
                           </div>
                         </div>
                       </div>
                      @else
                        No Articles for Today
                     @endif
                    @endforeach
                @endforeach

回答by Kiran Subedi

If you are getting data using ORM in laravel like

如果您在 laravel 中使用 ORM 获取数据,例如

$data = \App\Model::get();

then you can check whether the $datais empty or not as following :

然后您可以检查是否$data为空,如下所示:

@if(!$data->isEmpty())
     // $data is not empty
@else
    // $data is empty
@endif

Reference: Check Here

参考检查这里

But if you use findor findOrFail, then you can check array as

但是如果您使用findor findOrFail,那么您可以将数组检查为

$data = \App\Model::findOrFail($id);


@if(!empty($data))
     // $data is not empty
@else
    // $data is empty
@endif

回答by Mamulasa

Well, the code is executed if the article is not empty. But The code in the else statement is still not executed. I still can't display the "no article" message it there are no articles available

好吧,如果文章不为空,则执行代码。但是else语句中的代码还是没有执行。我仍然无法显示“没有文章”消息,因为没有可用文章

  @if(!$restaurantmenue->isEmpty())
                    @foreach($restaurantmenue as $daily)
                        @foreach($daily->articles as $menue)

                           <div class="card card-horizontal">
                             <div class="row">
                               <div class="col-md-5">
                                 <div class="image" style="background-image: url({{asset('images/frontend/profile/profileHero.jpg')}}); background-size: cover; background-position: center center;">
                                   <img src="{{asset('images/frontend/profile/profileHero.jpg')}}" alt="..." style="display: none;">
                                   <div class="filter filter-azure">
                                     <button type="button" class="btn btn-neutral btn-round">
                                         <i class="fa fa-heart"></i> SM?CKT MIR
                                     </button>
                                   </div>
                                 </div>
                               </div>
                               <div class="col-md-7">
                                    <div class="content">
                                      <p class="category text-info">
                                           <i class="fa fa-trophy"></i> Best of
                                       </p>

                                       <a class="card-link" href="#">
                                           <h4 class="title">{{$menue->title}} </h4>
                                       </a>
                                       <a class="card-link" href="#">
                                           <p class="description">{{$menue->body}}</p>
                                           <br />
                                           {{$menue->price}} 
                                       </a>
                                        <div class="footer">
                                           <div class="stats">
                                               <a class="card-link" href="#">
                                                  <i class="fa fa-male"></i> {{$restaurant->name}}
                                               </a>
                                           </div>
                                           <div class="stats">
                                             <a class="card-link" href="#">
                                               <i class="fa fa-comments"></i> 23 Comments
                                             </a>
                                           </div>
                                           <div class="stats">
                                              <a class="card-link" href="#">
                                               <i class="fa fa-heart"></i> 231 Likes
                                              </a>
                                           </div>
                                       </div>
                                   </div>
                               </div>
                             </div>
                           </div>



                        @endforeach
                    @endforeach
                  @else
                    no article
                @endif

回答by Terabyte

Hope this might help you.

希望这可以帮助你。

$processes = [1, 2, 4, 5];
$maintenance = [1, 2, 3, 4, 5];

$array_value = function ($value, $array) {
    $str = "$value";
    if (in_array($str, $array)) {
        echo 5;
    } else {
        echo 1;
    }
};

foreach ($maintenance as $m) {
    $array_value ($m, $processes);
}

Basically I'm comparing two arrays and display 5 if data found on the other array

基本上我比较两个数组并显示 5 如果在另一个数组上找到数据