1

I have the following json :

{"returnCode":0,"returnMessage":"SUCCESS","data":{"OrganizationName":"ABC inc."}}

I wrote the following code to parse the organization name. But it is not working.

$response = json_decode($server_output, true);
 foreach($response['data'] as $item)
 {
   echo $item['OrganizationName'];

}
1
  • 2
    THe issue is data isn't an array. You need to access it like an object $response->data->OrganizationName Commented May 30, 2016 at 6:26

4 Answers 4

2

i think json_decode() function work for you.

$array= curl_exec ($ch);
$server_output =json_decode($array);
$server_output->data->OrganizationName // output ABC inc.
Sign up to request clarification or add additional context in comments.

1 Comment

please see my edit. I have made the change but it is returning only the first letter A of organization.
1

No need to use foreach use below code to get OrganizationName

 $response = json_decode($server_output, true);
 $response['data']['OrganizationName'];

Comments

0

If you want to access it as an array, typecast it.

$response = (array) json_decode($server_output);

Comments

0

There is no need to foreach you can directly access the Organization name.

Check below code

$server_output='{"returnCode":0,"returnMessage":"SUCCESS","data":{"OrganizationName":"ABC inc."}}';

$response = json_decode($server_output);

echo $response->data->OrganizationName; // ABC inc.

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.