So I'm trying to parse an array of results I get back from a SOAP query. However PHP seems to be giving me grief (more likely I'm giving myself grief). I want to echo out all the user objects that are retrieved from the query.
Results from query:
object(stdClass)#3 (1) { ["row"]=> array(4) {
[0]=> object(stdClass)#4 (2) { ["pkid"]=> string(36) "0c69d6a4-a4a7-0a68-ac47-e3b9279fb25e" ["userid"]=> string(7) "Kitchen" }
[1]=> object(stdClass)#5 (2) { ["pkid"]=> string(36) "60c858b6-71fd-4c6b-0bc7-18c56a07127e" ["userid"]=> string(7) "Control" }
[2]=> object(stdClass)#6 (2) { ["pkid"]=> string(36) "b59301fc-a197-6d50-b217-7d8967332601" ["userid"]=> string(5) "House" }
[3]=> object(stdClass)#7 (2) { ["pkid"]=> string(36) "dcf8b18d-cf95-4ffd-85d2-a6f9b45f5fc9" ["userid"]=> string(47) "Token_User_4806a7a1-f23d-4647-b46c-f62ad66452a0" } } }
Code for result:
$response = $client->executeSQLQuery(array("sql"=>"select pkid,userid from enduser"));
// var_dump($response);
foreach ($response as $row){
var_dump($row);
}
Now I can get one deeper into the array by altering the code as follows:
foreach ($response as $row){
var_dump($row->row);
}
However this doesn't work to access just the userid element:
foreach ($response as $row){
var_dump($row->row->userid);
}
PHP instead returns:
PHP Notice: Trying to get property of non-object
Any help is much appreciated, thanks.
Just a quick update: Source query:
$response = $client->executeSQLQuery(array("sql"=>"select pkid,userid from enduser"));
// var_dump($response);
foreach ($response as $row){
var_dump($row);
//echo("USER: ".$row->userid)."<br>";
}
var_dump of $row returns:
object(stdClass)#3 (1) { ["row"]=> array(4) { [0]=> object(stdClass)#4 (2) { ["pkid"]=> string(36) "0c69d6a4-a4a7-0a68-ac47-e3b9279fb25e" ["userid"]=> string(7) "Kitchen" } [1]=> object(stdClass)#5 (2) { ["pkid"]=> string(36) "60c858b6-71fd-4c6b-0bc7-18c56a07127e" ["userid"]=> string(7) "Control" } [2]=> object(stdClass)#6 (2) { ["pkid"]=> string(36) "b59301fc-a197-6d50-b217-7d8967332601" ["userid"]=> string(5) "House" } [3]=> object(stdClass)#7 (2) { ["pkid"]=> string(36) "dcf8b18d-cf95-4ffd-85d2-a6f9b45f5fc9" ["userid"]=> string(47) "Token_User_4806a7a1-f23d-4647-b46c-f62ad66452a0" } } }
var_dump of $response returns:
object(stdClass)#2 (1) { ["return"]=> object(stdClass)#3 (1) { ["row"]=> array(4) { [0]=> object(stdClass)#4 (2) { ["pkid"]=> string(36) "0c69d6a4-a4a7-0a68-ac47-e3b9279fb25e" ["userid"]=> string(7) "Kitchen" } [1]=> object(stdClass)#5 (2) { ["pkid"]=> string(36) "60c858b6-71fd-4c6b-0bc7-18c56a07127e" ["userid"]=> string(7) "Control" } [2]=> object(stdClass)#6 (2) { ["pkid"]=> string(36) "b59301fc-a197-6d50-b217-7d8967332601" ["userid"]=> string(5) "House" } [3]=> object(stdClass)#7 (2) { ["pkid"]=> string(36) "dcf8b18d-cf95-4ffd-85d2-a6f9b45f5fc9" ["userid"]=> string(47) "Token_User_4806a7a1-f23d-4647-b46c-f62ad66452a0" } } } }
var_dump($row)is an object containing an array property calledrow. Each of its elements is an object, with apkidanduseridproperty. So$row->row[0]->useridfor example, to get the first. Orforeach ($row->row as $user)foreach ($response->row as $row)I end up getting back PHP Warning: Invalid argument supplied for foreach(). You are correct though if I do$row->row[0]->useridit does in fact return the first item in the array. However I want it to keep looping for each item in the array and return them all.var_dump($response)? Please edit to post the output ofvar_dump($response)copied from the browser page source, so all the indentation and line breaks are left intact.$row(sounds like maybe it is) then you needforeach ($row->row as $item) { echo $item->userid; }var_dump. I think I see where I'm getting confused. I need to loop foreach of the objects which in my case is 0, 1,2,3 etc. to return the property of userid. However each time I use the->I end up just adding another array object namedrow. For example If I doforeach ($response as $row->row->row)I will end up with two morerowarray objects with only one item which is the next array.