D
D
danyarob2021-11-14 01:27:51
PHP
danyarob, 2021-11-14 01:27:51

What causes an error when changing the value in json?

JSON:

"USD": {
        "Value": 71.4783,
        "Previous": 71.0863
    },


PHP:
$data  = file_get_contents('values.json');
    $data = json_decode($data , true); 
    $data['USD']['Value'] = "2";
    $data = json_encode($data);
    file_put_contents('values.json', $data);


Error: Uncaught Error: Cannot use string offset as an array in

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Aetae, 2021-11-14
@Aetae

I will now show you an absolutely secret piece of code, you will not find it anywhere else. This miracle solves 99% of the problems!

spoiler
$data  = file_get_contents('values.json');
$data = json_decode($data , true); 
var_dump($data);

N
no_one_safe, 2021-11-14
@no_one_safe

It looks like you have an extra comma in your JSON file. In the end.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question