L
L
Let_peace2020-08-14 12:45:45
PHP
Let_peace, 2020-08-14 12:45:45

How to change data in JSON file using PHP?

There is a JSON file like this: {"name" : "value1"} .

Using PHP, I get the value of value1 , somehow change it (it doesn’t matter) and I want to push the already changed value of value1_new into JSON so that I get a file like: {"name" : "value1_new"} .

That is, if there was {"name" : "Ivan"} , so that it becomes {"name" : "Vasiliy"} after my manipulations.

PHP:

<?php
$json = file_get_contents('./json_file.json');
$json = json_decode($json, true);
$name = $json['name'];
// манипуляции с $name
// ...
// Например, было $name="Ivan", стало $name="Vasiliy"
// и тут нужно в JSON закинуть уже измененный $name


I don’t understand something in any way how to do this, so I ask for help from intelligent colleagues))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-08-14
@Let_peace

$data = file_get_contents('data.json');
$data = json_decode($data, true);
$data['name'] = 'Vasiliy';
$data = json_encode($data);
file_put_contents('data.json', $data);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question