-1

I need to show only the status value of this string but I dont know how to do:

Web Object ( 
    [data] => stdClass Object ( 
        [operations] => stdClass Object ( 
            [384322232931] => stdClass Object ( 
                [status] => on
            ) 
        ) 
    ) 
    [error] => 
)

Thanks for read

Barmar
  • 741,623
  • 53
  • 500
  • 612
Daniel
  • 3
  • 1

3 Answers3

3

If the object is in $webobj, it would be:

$webobj->data->operations->{'384322232931'}->status

You can't use normal property syntax when the property name is numeric, you need the braces and quotes, as described here: How to access object properties with names like integers?

If that number is the account ID, you can do:

$webobj->data->operations->{$account_id}->status

where $account_id contains your account number.

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • And if the {'384322232931'} is a random number? – Daniel Nov 08 '14 at 22:02
  • Is this coming from JSON? It would probably be easier if you used `json_decode($json, true)` so you get an array instead of an object. Then you can use `foreach()` to iterate over all the elements of `operations`. – Barmar Nov 08 '14 at 22:04
  • I get this response from this php sdk: https://github.com/ElevenPaths/latch-sdk-php from $statusResponse = $api->status(ACCOUNT_ID_HERE); – Daniel Nov 08 '14 at 22:05
0

I believe the number 384322232931 is "random", if so, you'll have to use a recursive method:

function captureStatus($data) {
    $obj = (array) $data;
    $key = key($obj);

    if ($key === 'status') {
        return $obj['status'];
    } else if ($key !== NULL) {
        return captureStatus($obj[$key]);
    }
    return NULL;
}

if not random, use the method of @Barmar

Protomen
  • 9,471
  • 9
  • 57
  • 124
-1

It looks like the property you want is embedded deeply, so the issue you're probably having is that you need to go through several children.

Try: $status = $yourObj->data->operations->384322232931->status;

I don't know if that helps. I'm a little confused what you mean by the "string" because what you posted looks like the print_r output for an object.

If what you're trying to do is extract that value from a string then I think you might be doing something wonky.