1

I have the following object array returned from a soap call:

$result = $this->soapClient->__soapCall($method, $arguments);

var_dump($result);


object(stdClass)#4 (1) {
    ["Jobs_GetResult"]=> object(stdClass)#5 (3) {
        ["Jobs"]=> array(4) {
              [0]=> object(stdClass)#7 (19) {
                    ["JobID"]=> int(55082846)
                    ["JobName"]=> string(18) "Fix xyz"
              } 

        }
        ["Errors"]=> object(stdClass)#10 (2) {
             ["Result"]=> int(0)
             ["Message"]=> string(0) "" 
        }
        ["RecordCount"]=> int(1) 
    }
}

I want to check if there are any errors - this is easy when the parent array key is known e.g:

if($result->Jobs_GetResult->Errors->Result > 0){
     // display message
}

The issue is I do not know what the name of the top level array key is going to be for most of the calls as i'm using a generic method - in the above example it's Jobs_GetResult so the above would work.

In instances where the top level array key is unknown how do I check if there are any errors returned?

In general the name of the parent array key is usually the name of the method call with Result appended to it. so I was thinking doing something along the lines of:

 if($result->$method . 'Result'->Errors->Result > 0){
     // display message
 }

But obviously the above syntax is incorrect. Any one know how to output value of $method and chain it to $result and append it with Result

Is there any other way I can check if the Errors array result is greater than 1 without knowing what the parent array key is?

1
  • 1
    Maybe a simple foreach can do the job? If you do foreach($result as $key => $value) {} you can get the error in $value->Errors->Result no? Commented Oct 18, 2018 at 12:03

1 Answer 1

3

Try this:

$result = $this->soapClient->__soapCall($method, $arguments);

$firstKey = key($result);

if (!empty($firstKey) && !empty($result->{$firstKey}->Errors->Result)) {
    // display message
}
Sign up to request clarification or add additional context in comments.

6 Comments

Can you use key() on an object? It doesn't look like it according to the manual.
You can cast as Std object to an array first then it will work as you expect.
@jeroen You dont actually need to cast it, just tried it on an object and it works. I was sceptical also
@RiggsFolly Nice, I didn't know that. Just tried it myself :-)
Daniel I was sceptical and that is why I tested it. Once I found it worked I upvoted your answer.
|

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.