Laravel:数组为null时的数组循环的集合

时间:2017-11-22 10:44:25

标签: php sql arrays laravel multidimensional-array

当我打印时,我收集了两个空数组:

     array:2 [▼
       0 => Collection {#419 ▼
            #items: []
          }
       1 => Collection {#412 ▼
          #items: []
       }
   ]

在这个空数组的计数中,我得到了" Count - 2" 。我想做的事 我的代码循环两次(这是我的计数)并检查是否 在ARRAY-1数据中可用,然后打印"可用数据" 打印"数据不可用"

这是我的代码:

由于数组的集合

,有两个foreach循环
   foreach($TestArr as $TestArr1) {
      foreach($TestArr1 as $item) {
         if($item->gallery_id == $val && $item->is_thumb_image == 1) {
            echo "YesImage";
         } else {
            echo "NoImage";
         }
     } 
  } 
  

这是完整的代码;

<div class="container">
         @if(!empty($galleryArr)) // Gt a folder name
           @foreach($galleryArr as $key => $val)
             <div class="col-md-3">
                <div class="service-block service1">
                 <?php 

                       foreach($TestArr as $TestArr1) { //set for thumb image 
                        if(count($TestArr1)){
                          foreach($TestArr1 as $item) {
                             if($item->gallery_id == $val && $item->is_thumb_image == 1) {
                                echo "YesImage";
                             } 
                          }
                        }else{
                          echo "NoImage";
                        }
                      } 


                    ?>


                    <h4>{{ $key }}</h4>
                    <a href="javascript:void(0);" class="btn" id="dynamic_{{$val}}">View Gallary</a>
                    <script type="text/javascript">
                                // added
                        $('#dynamic_{!! $val !!}').on('click', function() {

                            $(this).lightGallery({
                                dynamic: true,
                                dynamicEl: [
                                <?php if(!empty($mediaData)){ 
                                    foreach($mediaData as $k => $v){ 
                                        if($v->gallery_id == $val){ ?>
                                        {
                                            "src": '{!! "/images/".$v->path !!}',
                                            'thumb': '{!! "/images/".$v->path !!}',
                                            'subHtml': '<h4>{{ $v->caption }}</h4>'
                                        },
                                <?php } } }?>
                                ]
                            })

                        });

                    </script>
                </div>
            </div>
            @endforeach
        @endif




</div>

1 个答案:

答案 0 :(得分:1)

检查array1是否为foreach,

   foreach($TestArr as $TestArr1) {
    if(count($TestArr1)){
      foreach($TestArr1 as $item) {
         if($item->gallery_id == $val && $item->is_thumb_image == 1) {
            echo "YesImage";
         }else{
             echo "NoImage";
         }
      }
    }else{
      echo "NoImage";
    }
  }