PHP Foreach Arrays and objects

AttikAttak picture AttikAttak · Dec 4, 2012 · Viewed 121.5k times · Source

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?

Answer

GBD picture GBD · Dec 4, 2012

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;
}