0

When I print i got collection of two null array as:

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

In a count of this null array I got "Count - 2". What i want to do with my code that loop two times (which is my count) and check that if in ARRAY-1 data is available then print "data available" else print "data not available".

Here is my code:

two foreach loop because of collection of array

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

Here is a full code;

<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>
5
  • 1
    So what's the problem ? Commented Nov 22, 2017 at 10:47
  • Problem is when i gt null array loop is not continue. Commented Nov 22, 2017 at 10:48
  • This is fact man, while looping through array/collection, if empty it will not continue. you can check size of each collection inside your array to determine value exist or not. Commented Nov 22, 2017 at 10:52
  • what i want if i gt two count in my array which is null or notNull ,my loop run two times.Is it possible.? Commented Nov 22, 2017 at 10:52
  • If you get count two then of course it's not null but inner collection can be empty Commented Nov 22, 2017 at 10:54

1 Answer 1

1

Check if array1 is empty befor 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";
    }
  } 
Sign up to request clarification or add additional context in comments.

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.