How do I convert object to array in PHP.
I have this code which I want to access its content using array
$tranx = json_decode($response);
My thought is it has been converted to array but am unable to access it using foreach loop
json_decode turns an JSON object string into an array, not an object.
have you validated what’s in $response?
Actually, it does when the second argument is not true
(and it’s false
by default).
json_decode($response)
returns objects (instances of stdClass
).
json_decode($response, true)
returns an array.
As per the documentation.
Sorry, I phrased that badly.
The point was that the input is a String, not an Object, so the title’s somewhat misleading.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.