PHP - json decode example

json to object
    
    $json = '{"a":1,"b":2,"c":[1,2]}';
    $out = json_decode($json);
    print_r($out);
    // output
    // stdClass Object
    // (
    //     [a] => 1
    //     [b] => 2
    //     [c] => Array
    //         (
    //             [0] => 1
    //             [1] => 2
    //         )
    // )
    
json to array
    
    $json = '{"a":1,"b":2,"c":[1,2]}';
    $out = json_decode($json, true);
    print_r($out);
    // output
    // Array
    // (
    //     [a] => 1
    //     [b] => 2
    //     [c] => Array
    //         (
    //             [0] => 1
    //             [1] => 2
    //         )
    // )
    

Comments