2
$b = 0;
foreach ($settings['items'] as $item){
    $b++;
    if ($b==1){
        $out .='First';
    } else{
        $out2 .='Second';
    }
 }

 echo '<div class="inner">'.$out.'</div>';
 echo '<div class="inner">'.$out2.'</div>';

which output

 <div class="inner">First</div>
 <div class="inner">Second Second Second Second</div>

But I would like to have this structure

 <div class="inner">First</div>
 <div class="inner">Second Second</div>
 <div class="inner">Second Second</div>

One probable solution that I thought is using array_slice But for first loop I only need one element not two, that's why I think I can not do that.

4
  • Where is $out2 come from? Commented Feb 4, 2019 at 11:57
  • 1
    Use the modulo operator on the loop counter. $b % 3 == 0 means first column, $b % 3 != 0 means second or third. (Requires starting your counter at 0 for the first item.) Commented Feb 4, 2019 at 11:58
  • Or split your array into chunks of size 3. Commented Feb 4, 2019 at 11:58
  • You can also get the first element, unset it, and slice the array into chunks of size 2. easy Commented Feb 4, 2019 at 12:25

4 Answers 4

1

Try this:

$first = true;
$all = [];
$couples = [];
foreach ($settings['items'] as $item){
    if($first){
        $first = false;
        $first_out = 'First'; //or whatever
        $all[] = $first_out;
    }else{
        if(count($couples) == 2){
            $all[] = $couples;
            $couples = [];
        }
        $couples[] = 'Second';
    }
}
foreach($all as $value){
    echo '<div class="inner">'.implode(',', $value).'</div>';
}
Sign up to request clarification or add additional context in comments.

Comments

0

A little pre-wrangling, remove the first item, and then merge it into the remaining chunked array.

<?php
$items   = array_fill(0, 5, 'foo'); // An array of five 'foo's.
$first   = array_shift($items);
$chunked = array_chunk($items, 2);
$chunked = array_merge([[$first]], $chunked);

var_dump($chunked);

foreach($chunked as $chunk)
{
    echo '<div class="inner">' . implode(' ', $chunk) . "</div>\n";
}

Output:

array(3) {
    [0]=>
    array(1) {
    [0]=>
    string(3) "foo"
    }
    [1]=>
    array(2) {
    [0]=>
    string(3) "foo"
    [1]=>
    string(3) "foo"
    }
    [2]=>
    array(2) {
    [0]=>
    string(3) "foo"
    [1]=>
    string(3) "foo"
    }
}
<div class="inner">foo</div>
<div class="inner">foo foo</div>
<div class="inner">foo foo</div>

Comments

0

Because you don't use value of $settings['items'] in the code, you can just work with that array length

$count = count($settings['items']);
if ($count--) echo `<div class="inner">First</div>`;
while($count > 1) {
   echo '<div class="inner">Second Second</div>';
   $count -= 2;
}
if ($count--) echo `<div class="inner">Second</div>`;

1 Comment

I will use $settings['items']
0

You can try this out,

$b = 0;
for($j = 1; $j <= 2; $j++){
    $b++;
    for($i = 1; $i <= $b;$i++){
        switch($b){
            case '1': echo '<div class="inner">FIRST</div>';break;
            case '2': echo '<div class="inner">SECOND SECOND</div>';break;
        }
    }
}

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.