-1

I'm using json_ecnode, and later json_decode in PHP. for some reason, even though i'm encoding an array with json_encode, it always comes out as an object after the json_decode decoding.

I have no control over the json_decode part as some other system is decoding it and using it. I'm the one dealing with the encoding part. what can I do to make it decode as an array?

here is in example code:

$types = array(
'aa1' => array('type' => 'document/unknown', 'icon' => 'unknown'),
'aa2' => array('type' => 'video/quicktime', 'icon' => 'quicktime', 'groups' => array('video'), 'string' => 'video') 
);

var_dump($types);

$typesencoded = json_encode($types);
var_dump($typesencoded);

$typesdecoded = json_decode($typesencoded);
var_dump($typesdecoded);

and this is the output:

/tests/test.php:28:array (size=2)
  'aa1' => 
    array (size=2)
      'type' => string 'document/unknown' (length=16)
      'icon' => string 'unknown' (length=7)
  'aa2' => 
    array (size=4)
      'type' => string 'video/quicktime' (length=15)
      'icon' => string 'quicktime' (length=9)
      'groups' => 
        array (size=1)
          0 => string 'video' (length=5)
      'string' => string 'video' (length=5)

/tests/test.php:35:string '{"aa1":{"type":"document\/unknown","icon":"unknown"},"aa2":{"type":"video\/quicktime","icon":"quicktime","groups":["video"],"string":"video"}}' (length=142)

/tests/test.php:40:
object(stdClass)[78]
  public 'aa1' => 
    object(stdClass)[77]
      public 'type' => string 'document/unknown' (length=16)
      public 'icon' => string 'unknown' (length=7)
  public 'aa2' => 
    object(stdClass)[79]
      public 'type' => string 'video/quicktime' (length=15)
      public 'icon' => string 'quicktime' (length=9)
      public 'groups' => 
        array (size=1)
          0 => string 'video' (length=5)
      public 'string' => string 'video' (length=5)
john_black
  • 167
  • 3
  • 14
  • 1
    `json_decode($typesencoded, true);` see manual, there is nothing you can do with the string to change how it decodes. – Nigel Ren Jun 19 '18 at 13:24
  • 1
    Possible duplicate of [How to convert JSON string to array](https://stackoverflow.com/questions/7511821/how-to-convert-json-string-to-array) – Nigel Ren Jun 19 '18 at 13:25
  • as I stated, I don't have control over the decoding part, I don't have control over the json_decode part. – john_black Jun 19 '18 at 13:27
  • It's up to the decoding code whether it prefers an object or an associative array. If you have no control over it, why to you care? – deceze Jun 19 '18 at 13:29

3 Answers3

1

When decoding, pass a second argument as TRUE. The second argument takes a boolean value i.e. TRUE/FALSE which will return an assoc array if TRUE and an object if FALSE. Change

$typesdecoded = json_decode($typesencoded);

to

$typesdecoded = json_decode($typesencoded,TRUE);
Rachitta A Dua
  • 358
  • 1
  • 11
0

An associative* PHP array can only be encoded to a JSON object, but a JSON object can be decoded to either an associative array or PHP object. You control which you prefer with the second parameter to json_decode. You cannot control the result on the encoding side, you need to control it on the decoding side.

* (non-continuously numerically indexed array)

The alternative is that the encoding side provides something that encodes to a JSON array ([...]), which can only decode to a PHP array.

deceze
  • 510,633
  • 85
  • 743
  • 889
-1

this is what solved this for me:

    $types = array(
     (object)array(
         'extension' => 'aa1',
         'icon' => 'unknown',
         'type' => 'document/unknown',
         'customdescription' => ''
     ),

    (object)array(
        'extension' => 'aa2',
        'icon' => 'quicktime',
        'type' => 'video/quicktime',
        'groups' => array('video'), 'string' => 'video'
    ),      
);

$typesencoded = json_encode($types);
john_black
  • 167
  • 3
  • 14