1

I am having difficulty while having the dual container div inside my for each array loop. Getting "two-half" div after each "wrap" div. But need to wrap "two-half" div with 2 "wrap" inside.

Expected:

    <div class="item">
     <div class="two-half">
      <div class="wrap">1</div>
      <div class="wrap">2</div>
     </div>
     <div class="two-half">
      <div class="wrap">3</div>
      <div class="wrap">4</div>
     </div>
    </div>
    <div class="item">
     <div class="two-half">
      <div class="wrap">5</div>
      <div class="wrap">6</div>
     </div>
     <div class="two-half">
      <div class="wrap">7</div>
      <div class="wrap">8</div>
     </div>
    </div>

Code:

<div class="item"> 
 <?php 
 $count = 1; 
 $array = array(1,2,3,4,5,6,7,8); 
 foreach($array as $item) { ?> 
  <div class="two-half"> 
   <div class="wrap"> 
     <?php echo $item; ?> 
   </div> 
  </div> 
<?php if ($count%4 == 0) { ?> 
  </div> 
<?php } $count++; } ?>

Please help me to get the expected output. Thanks!

7
  • 1
    For starters, you have the $item and $array variables backwards in the foreach Commented Feb 6, 2020 at 16:35
  • I have corrected syntax. I need the logic inside the loop. Please help with that if you knows. Commented Feb 6, 2020 at 16:41
  • whats your actual? Commented Feb 6, 2020 at 16:42
  • Actual getting two-half div after every wrap. I need to wrap the two-half div for 2 wrap inside the item div. Commented Feb 6, 2020 at 16:46
  • 1
    why are using $count%4, you need to use $count%2, and start count with 0 Commented Feb 6, 2020 at 17:17

1 Answer 1

3

Use array_chunk to split an array into chunks.

<?php
$array = range(1, 8);
$half  = array_chunk($array, 2); // chunk for halfs
$item  = array_chunk($half, 2);  // chunk for item
$count = 1;
?>

{{-- Create div-item --}}
<?php foreach ($item as $parent) {?>
    <div class="item">

        {{-- Create div-half --}}
        <?php foreach ($parent as $half) {?>
        <div class="two-half">

            {{-- Create div-wrap --}}
            <?php foreach ($half as $item) {?>
                <div class="wrap"><?php echo $count; ?></div>
                <?php $count++;?>
            <?php }?>

        </div>
        <?php }?>

    </div>
<?php }?>

Result

<div class="item">
    <div class="two-half">
        <div class="wrap">1</div>
        <div class="wrap">2</div>
    </div>
    <div class="two-half">
        <div class="wrap">3</div>
        <div class="wrap">4</div>
    </div>
</div>

<div class="item">
    <div class="two-half">
        <div class="wrap">5</div>
        <div class="wrap">6</div>
    </div>
    <div class="two-half">
        <div class="wrap">7</div>
        <div class="wrap">8</div>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

The element in the array can be infinite. I need the item div with 4 wrap inside. Your answer is good for static 8 element array. But I need it for (any amount) of elements in the array.
It can be infinite. Try to change range(from, to);

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.