passing object to a view within a foreach loop in CodeIgniter

33 Views Asked by At

I'm working with CodeIgniter, passing an object to a view within a foreach loop. I've noticed that 'view2' prints the number '2' during the first iteration of the loop, but I can't determine why it also continues to print '2' during the second iteration.

Is this behavior due to the objects sharing memory addresses, or is it an issue with the variables used within the loop?

Controller

function controller() {
    $result = (object) [
        (object) [ "a" => 1, "b" => 2 ],
        (object) [ "a" => 3 ]
    ];

    $this->load->view("view1", $result);
}

view1

<?php 
    foreach($result as $index => $res) {
        if($index == 0) {
            $this->load->view("view2", $res);
        } else {
            $this->load->view("view2");
        }
    }

?>

view2

echo $b
0

There are 0 best solutions below