Why cant I echo this responce from API

this is the code

$response =  json_decode($response, JSON_PRETTY_PRINT);
echo $response

error received

Warning : Array to string conversion in

Because it’s an array, not a string.

I know but am I missing something?

You… literally have a comment on line 4 telling you that it’s translating the array into a string.

json_encode : Input array, get string.
json_decode : Input string, get array.

echo cannot print array without complaining.

Your first post makes $response an array, then tries to echo it.
Your second post makes $jsonData a string, and then echo’s it.

Completely different scenarios.

PS: PHP should also be barking at you about trying to use JSON_PRETTY_PRINT as a Decode flag; an array does not have such a concept. It’s… actually probably counting it as the associative parameter, which is why you’re getting an associative array rather than an object…

2 Likes

I see that now. I completely missed “decode” “encode” differences. My bad

2 Likes

Seems like things are sorted out here, right?

yes thank you @lara

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.