tsdesai Posted January 15, 2016 Share Posted January 15, 2016 Hi, I am trying to loop through using php inorder to get the elements $names= $names>GetResult;foreach ($names as &$name){ echo $name->ID;} The above code gives me an error Notice: Trying to get property of non-object But if i try to access as below with the index number, it works. foreach ($names as &$name){ echo $name[0]->ID;} The problem with the above method is it will just give one, but i want to loop through and get all the contents. I know its something really basic that i ma missing but would appreciate your help. Many Thanks, T Quote Link to comment Share on other sites More sharing options...
requinix Posted January 15, 2016 Share Posted January 15, 2016 Looks like you have arrays within arrays. Do two foreachs. And figure out why you thought there was only one array of stuff because that suggests you aren't completely aware of what is going on. Quote Link to comment Share on other sites More sharing options...
tsdesai Posted January 15, 2016 Author Share Posted January 15, 2016 The thing is i am getting the result from an external web service, I am just trying to figure out how i can best consume it. Thanks, Quote Link to comment Share on other sites More sharing options...
requinix Posted January 15, 2016 Share Posted January 15, 2016 What's the structure of the data being returned, and what is your code to load it into objects? Quote Link to comment Share on other sites More sharing options...
tsdesai Posted January 15, 2016 Author Share Posted January 15, 2016 (edited) Hi, I have just done print_r($names) and it returns back below. $names= $names->GetResult; print_r($names); and it returns below. stdClass Object ( [Reult] => Array ( [0] => stdClass Object ( [iD] => 1 [testid2] => 2 [TypeID] => 4 [Title] => test [Description] => test [Date] => 2016-01-07 ) [1] => stdClass Object ( [iD] => 2 [testid2] => 3 [TypeID] => 5 [Title] => test22 [Description] => test222 [Date] => 2016-01-07) Thanks Edited January 15, 2016 by tsdesai Quote Link to comment Share on other sites More sharing options...
Solution Muddy_Funster Posted January 15, 2016 Solution Share Posted January 15, 2016 So what you have is an object containing an array of objects, you need to iterate through each level. so: $nameObj = $names->GetResult; foreach($nameObj as $nameArray){ foreach($nameArray as $dataObj){ echo $dataObj->ID; } } Quote Link to comment Share on other sites More sharing options...
tsdesai Posted January 15, 2016 Author Share Posted January 15, 2016 Brilliant !!!! Thank you so much. Everything seems to be happy now. R, T Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted January 15, 2016 Share Posted January 15, 2016 You're welcome, glad we could help. Could you mark the thread as Answered if you are happy that there is nothing else to cover? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.