0

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" } } } }
5
  • Your outermost var_dump($row) is an object containing an array property called row. Each of its elements is an object, with a pkid and userid property. So $row->row[0]->userid for example, to get the first. Or foreach ($row->row as $user) Commented Oct 22, 2014 at 13:29
  • When I attempt your suggestion 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]->userid it 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. Commented Oct 23, 2014 at 0:21
  • The object posted above at the top is var_dump($response)? Please edit to post the output of var_dump($response) copied from the browser page source, so all the indentation and line breaks are left intact. Commented Oct 23, 2014 at 0:24
  • It's just a matter of which variable name the object dumped at the top is. If that's $row (sounds like maybe it is) then you need foreach ($row->row as $item) { echo $item->userid; } Commented Oct 23, 2014 at 0:25
  • So I added the additional 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 named row. For example If I do foreach ($response as $row->row->row) I will end up with two more row array objects with only one item which is the next array. Commented Oct 23, 2014 at 0:47

1 Answer 1

1

I'm dumb, this is the same problem I had in another post. I basically need to access the return->row objects then I'm able to access the pkid or userid properties in the array.

Final code looks like:

$response = $client->executeSQLQuery(array("sql"=>"select pkid,userid from enduser"));
//        var_dump($response);
foreach ($response->return->row as $row){
//        var_dump($row->userid);
echo("USER: ".$row->userid)."<br>";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.