K
K
kr_ilya2018-09-29 16:46:34
PHP
kr_ilya, 2018-09-29 16:46:34

How to add key-value pair to json array in php?

there is a json file with such content how to add values ​​to such an array in php? For example, add AUDUSD with value 22 I know how to start and end, but how to directly add values ​​so that the array structure is preserved.
{"EURUSD":25,"GBPCAD":10,"EURCAD":18}

$file1 = file_get_contents('data.json');
$list1 = json_decode($file1,TRUE); 
unset($file1);
$result = [];

//здесь должно быть добавление записи.

file_put_contents('data.json',json_encode($result));
echo "Успех";

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Antony Tkachenko, 2018-09-29
@kr_ilya

$list1['AUDUSD'] = 22;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question