0

Can you anyone show me the correct syntax to echo a specific element from the snippet below? I wish to echo the accountName element. How about the employeeId element?

A part of the output of var_dump($api_response):

object(stdClass)#1 (3) {
  ["responseCode"]=>
  string(1) "0"
  ["responseObject"]=>
  array(1) {
    [0]=>
    object(stdClass)#2 (50) {
      ["accountId"]=>
      int(429987)
      ["firmId"]=>
      int(129564)
      ["accountName"]=>
      string(12) "Acme"
      ["description"]=>
      string(0) ""
      ["website"]=>
      string(0) ""
      ["tickerSymbol"]=>
      string(0) ""
      ["rating"]=>
      string(0) ""
      ["assignedTo"]=>
      object(stdClass)#3 (33) {
        ["employeeId"]=>
        int(31142)

1 Answer 1

2

Notice that responseObject is an array, so use the array index to access the element(s) in it.

echo $api_response->responseObject[0]->accountName;
echo $api_response->responseObject[0]->assignedTo->employeeId;

if the responseObject contains multiple elements, use the foreach loop:

foreach($api_response->responseObject as $p)
{
    echo $p->accountName;
    echo $p->assignedTo->employeeId;
}
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.