0

Im looking for a solution to add a + number to my iteration and keep the numeration after each loop end, what i have done now is the next, but this give me only a numeration start>end.

I have 3 rows, each row contain data, one of them will be inserted in the middle and will add an iteration number+.

$data = array(
    array(
        'id' => 1,
        'data' => 'Good',
    ),
    array(
        'id' => 2,
        'data' => 'Equal',
    ),
    array(
        'id' => 3,
        'data' => 'Equal',
    )
);

$i = 1;
$concat = '';

foreach($data as $item){
    $count = $i++;

    $concat .= 'Nr. '.$count.' - Reg 1' . "\n";
    $concat .= 'Nr. '.$count.' - Reg 1' . "\n";
    $concat .= 'Nr. '.$count.' - Reg 1' . "\n";

    if($item['data'] == 'Good'){
        //count Nr Reg 1 + 1 
        $concat .= 'Nr. '.$count.' - Reg 2' . "\n";
        $concat .= 'Nr. '.$count.' - Reg 2' . "\n";
    }

    //count Nr Reg 1 + Reg 2 if exist + 1 
    $concat .= 'Nr. '.$count.' - Reg 3' . "\n";
    $concat .= 'Nr. '.$count.' - Reg 3' . "\n";
}

echo '<pre>';
echo $concat;

/*
resut:
Nr. 1 - Reg 1
Nr. 1 - Reg 1
Nr. 1 - Reg 1
Nr. 1 - Reg 2
Nr. 1 - Reg 2
Nr. 1 - Reg 3
Nr. 1 - Reg 3

Nr. 2 - Reg 1
Nr. 2 - Reg 1
Nr. 2 - Reg 1
Nr. 2 - Reg 3
Nr. 2 - Reg 3

Nr. 3 - Reg 1
Nr. 3 - Reg 1
Nr. 3 - Reg 1
Nr. 3 - Reg 3
Nr. 3 - Reg 3

the result i need is:

Nr. 1 - Reg 1
Nr. 1 - Reg 1
Nr. 1 - Reg 1
Nr. 2 - Reg 2
Nr. 2 - Reg 2
Nr. 3 - Reg 3
Nr. 3 - Reg 3

Nr. 4 - Reg 1
Nr. 4 - Reg 1
Nr. 4 - Reg 1
Nr. 5 - Reg 3
Nr. 5 - Reg 3

Nr. 6 - Reg 1
Nr. 6 - Reg 1
Nr. 6 - Reg 1
Nr. 7 - Reg 3
Nr. 7 - Reg 3
*/
4
  • 1
    Just add $count++ before each change of "reg"? Commented Oct 26, 2015 at 13:09
  • What i need is to store the value i added to the first iteration if Good exists!, then add to the Reg 1 + value i have added to Reg 2. Commented Oct 26, 2015 at 13:13
  • can you add an example, what is the expected result, and what you are currently getting Commented Oct 26, 2015 at 13:25
  • Is in my question the result it give me, and the result i need. Commented Oct 26, 2015 at 13:31

1 Answer 1

1

I don't understant what exactly you want but to get result like in your comment you must change you code like that:

$data = array(
    array(
        'id' => 1,
        'data' => 'Good',
    ),
    array(
        'id' => 2,
        'data' => 'Equal',
    ),
    array(
        'id' => 3,
        'data' => 'Equal',
    )
);

$count = 0;
$concat = '';

foreach($data as $item){
    ++$count;
    $concat .= 'Nr. '.$count.' - Reg 1' . "\n";
    $concat .= 'Nr. '.$count.' - Reg 1' . "\n";
    $concat .= 'Nr. '.$count.' - Reg 1' . "\n";

    if($item['data'] == 'Good'){
        ++$count;
        //count Nr Reg 1 + 1 
        $concat .= 'Nr. '.$count.' - Reg 2' . "\n";
        $concat .= 'Nr. '.$count.' - Reg 2' . "\n";
    }

    //count Nr Reg 1 + Reg 2 if exist + 1
    ++$count;
    $concat .= 'Nr. '.$count.' - Reg 3' . "\n";
    $concat .= 'Nr. '.$count.' - Reg 3' . "\n";
}

//result:

Nr. 1 - Reg 1
Nr. 1 - Reg 1
Nr. 1 - Reg 1
Nr. 2 - Reg 2
Nr. 2 - Reg 2
Nr. 3 - Reg 3
Nr. 3 - Reg 3
Nr. 4 - Reg 1
Nr. 4 - Reg 1
Nr. 4 - Reg 1
Nr. 5 - Reg 3
Nr. 5 - Reg 3
Nr. 6 - Reg 1
Nr. 6 - Reg 1
Nr. 6 - Reg 1
Nr. 7 - Reg 3
Nr. 7 - Reg 3
Sign up to request clarification or add additional context in comments.

3 Comments

Is what i want, the only thing is that on //count Nr Reg 1 + 1 it give me 2 and 3 then it goes to 5
I have edited your question and i've added the result you give me and what i need.
Then I have edited it too and I think this result is same what you write in you question.

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.