1

and trying to add an extra row(key : value). so this is my loops:

$newarray = [];
foreach($allJobs as $allJob)
{
 for($i = 0; $i < 2 ; $i++){
    $allJob->bku = $i;
    $newarray[] = $allJob;
 }
}

output now:

        Array
(
[0] => stdClass Object
    (
        [id] => CFF9B1A6-37B8-4000-B058-03DC648B5289
        [name] => Kreditor rapporter til RE pkt. 5 
        [bku] => 1
    )

[1] => stdClass Object
    (
        [id] => CFF9B1A6-37B8-4000-B058-03DC648B5289
        [name] => Kreditor rapporter til RE pkt. 5 
        [bku] => 1
    )

but as you can se the last key value pair is the same - [bku] => 1;

What I want:

      Array
(
[0] => stdClass Object
    (
        [id] => CFF9B1A6-37B8-4000-B058-03DC648B5289
        [name] => Kreditor rapporter til RE pkt. 5 
        [bku] => 0
    )

[1] => stdClass Object
    (
        [id] => CFF9B1A6-37B8-4000-B058-03DC648B5289
        [name] => Kreditor rapporter til RE pkt. 5 
        [bku] => 1
    )

So it increment my extra row bku in the nested loop.

1 Answers1

1

So I found a solution:

I had convert it from stdClass object to an array, and I did it like this:

$array = json_decode(json_encode($allJobs), true);
$newarray = [];

foreach($array as $allJob) {
 for($i = 0;  $i < 2 ; $i++){
     $allJob['bku'] = $i;
     $newarray[] = $allJob;
     }
}

Then it count correct in my nested loop.