Search code examples
phpdynamic-arrays

Access object properties within array


Using an API from a third party app, I use an API call:

print_r($response->response());

the result:

  (
        [0] => stdClass Object
            (
                [id] => 41
                [id_format] => 2014-{num}
                [id_value] => 530
                [client_id] => 37
                [date_billed] => 2014-08-13 20:12:44
                [date_due] => 2014-08-13 20:12:44
                [date_closed] => 
                [date_autodebit] => 
                [status] => active
                [subtotal] => 15.0000
                [total] => 15.0000
                [paid] => 0.0000
                [previous_due] => 0.0000
                [currency] => USD
                [note_public] => 
                [note_private] => 
                [id_code] => 2014-530
                [delivery_date_sent] => 2014-08-13 20:15:10
                [client_id_code] => 1522
                [client_first_name] => Nate
                [client_last_name] => TestAccount
                [client_company] => 
                [client_address1] => 
                [client_email] => [email protected]
                [due] => 15.0000
            )

    )

I am trying to access these elements to no avail. I have tried the following so far:

$test =  $response->response()[0]->id;
$test =  $response->response(0)->id;
$test =  $response->response()->[0]->id;
$test =  $response->response()->[0]id;

I cannot seem to figure out the syntax to set an element to a variable.

Please do not flame, I am trying my best and would appreciate an extra set of eyes to help my stupidity.


Solution

  • Try,

    $data = $response->response(); 
    echo $data[0]->id;