Search code examples
phparraysstdclass

PHP Foreach Arrays and objects


I have an array of objects; running print_r() returns the output below;

Array
(
    [0] => stdClass Object
        (
            [sm_id] => 1
            [c_id] => 1
        )
    [1] => stdClass Object
        (
            [sm_id] => 1
            [c_id] => 2
        )
)

How to loop through the result and access the student class objects?


Solution

  • Use

    //$arr should be array as you mentioned as below
    foreach($arr as $key=>$value){
      echo $value->sm_id;
    }
    

    OR

    //$arr should be array as you mentioned as below
    foreach($arr as $value){
      echo $value->sm_id;
    }