0
votes

i am trying to iterate a result set using foreach loop. Below is my array when i do print_r on result set

Array
(
    [0] => stdClass Object
        (
            [xml] =>                                            
            [qid] => 406
            [title] => Q by Sikander
            [description] => test question created by Sikander 
        ) 
)

Using following foreach loop

  foreach ($xmls_nodes as $value){
       echo $value->qid ;
       echo $value->xml ;
       echo $value->title ;
       echo $value->description ;
  }

now as a result it prints following

406 Q by Sikander test question created by Sikander

but right after this it also prints following for each of the record and row

A PHP Error was encountered Severity: Notice

Message: Trying to get property 'qid' of non-object

it give same line no in for error where i am printing these values and using die statement after this, please help me to get rid of this issue

2
You may use your foreach like this foreach ($xmls_nodes as $key => $value){ }Elangovan Selvaraj

2 Answers

1
votes

You have to convert this object to array and then just use it as array:

$arr = json_decode(json_encode($xmls_nodes), true);
1
votes

You have to do JSON-encode your object and then decode it back to an array [0] => stdClass Object

$arr = json_decode(json_encode($xmls_nodes), true);

then loop through it