We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Wrong decode from Json Frontend

Hello All.

In my project I try to use Cache. And I setup Frontend as Phalcon\Cache\Frontend\Json.

When I save variable (variable data as key-value array), all ok. But when I get this data from cache I receive stdObject. Thats not good.

Thank you.

It's also unavoidable. JSON and javascript don't have associative arrays, only numeric arrays. If you have an associative array in PHP, it will be converted to an object when you encode it as JSON. This is unavoidable.



8.1k
<?php
$json = '{"key_one":"value_for key_one","key_another":2}';

var_dump(json_decode($json));
var_dump(json_decode($json, true));
class stdClass#1 (2) {
  public $key_one =>
  string(17) "value_for key_one"
  public $key_another =>
  int(2)
}
array(2) {
  'key_one' =>
  string(17) "value_for key_one"
  'key_another' =>
  int(2)
}

in frontend you can use javascript function JSON.parse(text [, reviver])

Thank you for reply.

I know about it, but what I must do if any data in Array and Objects. About JSON this is unexpected behavior.

Thank you.