You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This line in BaseApi.php: $this->storeOutput = (array) json_decode($response);
takes a string value (a JSON string) and produces a result that is not completely an array.
The json_decode($response) function returns a PHP StdClass object. But then that result is cast to an array-type variable using (array).
In PHP, when casting an object to an array, only the first level of nested data is converted to array elements. In the deeper levels of nested data, any StdClass objects there remain as objects, the cast does not convert those to arrays.
So when writing code to traverse the returned data structure, one has to switch from array notation $arr['index'] to object notation $object->index, depending on which level of nesting you are in.
If the returned value of getData() method (for outputFormat json) is supposed to return an object, then it should be coded: $json_object = json_decode($response);
If it is meant to return an array, then a cast should not be used. The 2nd parm of the function should be used to ask for a true array: $json_array = json_decode($response, true);
EDITED 2024-05-15
When output format = 'json', my preference is to simply return the JSON string as is, which the feed produces. Let the caller of getData() convert the returned JSON string to an array or object as they prefer, in either case, a simple one-liner using json_decode(). Then everybody wins?
But updating the master version like that could break existing user PHP code because the returned value would now be a PHP (JSON) string (per my suggestion), whereas it used to return an array (which contains objects). It could be introduced major version number change like wrapper version 3.0.0.
The text was updated successfully, but these errors were encountered:
This line in BaseApi.php:
$this->storeOutput = (array) json_decode($response);
takes a string value (a JSON string) and produces a result that is not completely an array.
The
json_decode($response)
function returns a PHP StdClass object. But then that result is cast to an array-type variable using(array)
.In PHP, when casting an object to an array, only the first level of nested data is converted to array elements. In the deeper levels of nested data, any StdClass objects there remain as objects, the cast does not convert those to arrays.
So when writing code to traverse the returned data structure, one has to switch from array notation
$arr['index']
to object notation$object->index
, depending on which level of nesting you are in.If the returned value of getData() method (for outputFormat json) is supposed to return an object, then it should be coded:
$json_object = json_decode($response);
If it is meant to return an array, then a cast should not be used. The 2nd parm of the function should be used to ask for a true array:
$json_array = json_decode($response, true);
EDITED 2024-05-15
When output format = 'json', my preference is to simply return the JSON string as is, which the feed produces. Let the caller of getData() convert the returned JSON string to an array or object as they prefer, in either case, a simple one-liner using json_decode(). Then everybody wins?
But updating the master version like that could break existing user PHP code because the returned value would now be a PHP (JSON) string (per my suggestion), whereas it used to return an array (which contains objects). It could be introduced major version number change like wrapper version 3.0.0.
The text was updated successfully, but these errors were encountered: