1

I've a result from a webservice's query and I would like to get some values from it. It works but I have PHP notice issues, so I'm probably doing something wrong.

This is the $items variable content :

stdClass Object ( [response] => stdClass Object ( [0] => stdClass Object ( [id] => 275 [corpid] => 16107 [name] => default [description] => [status] => ok [nbSteps] => 7 ) [defaultItem] => 275 ) [error] => [status] => success ) 

So I tried something like :

foreach ( $items->response AS $key => $item ) { if ( $item->name == 'default' ){ // Line 106 $Id = $item->id; } } 

It works, $Id is equal to 275 but PHP returns a notice :

Notice: Trying to get property of non-object in /home/web/dev/webservice-form.php on line 106 

Any help would be greatly appreciated.

EDIT : This is the content of the $item variable (taken from the foreach loop) :

stdClass Object ( [id] => 275 [corpid] => 16107 [name] => default [description] => [status] => ok [nbSteps] => 7 ) 275 

Please note that the '275' is a part of the result.

4
  • 2
    Which line is 106, That's where the notice is being thrown? Commented Feb 26, 2013 at 13:10
  • Best way to debug try echo the item and item->id. I think you can convert to array and use values. Commented Feb 26, 2013 at 13:10
  • 2
    "defaultItem"->name. defaultItem is not an object. Commented Feb 26, 2013 at 13:10
  • Edited and added the "Line 106" Commented Feb 26, 2013 at 13:15

2 Answers 2

2

The problem is the defaultItem entry in your inner object. Your loop will at some point reach this and try to access name, which doesn't exist, because there is no object.

Should be easily solveable with is_object().

Sign up to request clarification or add additional context in comments.

Comments

2

You have mixed types, one being an objects, and one an int value, try checking what each item is:

foreach ( $items->response AS $key => $item ) { if(is_object($item) && $item->name == 'default'){ // Line 106 $Id = $item->id; } else { $Id = $item; // assume it's scalar value } } 

Obviously it would depend on what else you can expect on what other check you need to add in there..

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.