Change json Value with PHP

Asked

Viewed 562 times

3

I have a json file where I am listing the information that interests me with PHP.

I’m using Lottie.js to run this. json as a gif/video, my idea is to change the messages in English to Portuguese, I’m able to list them from json, but I don’t know how to change, follow my code:

<?php
$url = 'js/data3.json';

$jsonStr = file_get_contents($url);
$jsonArr = json_decode($jsonStr, true);


$titulo = array();


foreach ($jsonArr['layers'] as $row) {

$titulo[] = $row['nm'];


 }
?> 

the result I’m having is:

Keep an eye out for an email 
Message sent
Send Message

(is bringing the texts)

How I Change Value Message sent for Mensagem enviada for example?

Link to the Json: https://jsoneditoronline.org/? id=e231e4df96f349f281002c689d84ab0f

1 answer

1


My first interaction with JSON with PHP, had already done something a little more complex with XML, but the idea is the same, the functions that change:

I saved json to a file .JSON in the same folder as the PHP file.

<?php
//abrindo o json externo
$json = json_decode(file_get_contents('teste.json'));

//Editando a linha que vc quer
$json->layers[1]->nm = "Mensagem Enviada";

//Salvando as edições
$json_editado = file_put_contents('teste.json',json_encode($json));

//Carregando json após ser salvo já editado
$json = json_decode(file_get_contents('teste.json'));
//Imprimindo json editado
var_dump($json);
?>

What may get in the way is that part of the json has Array, and may get in the way when it comes to the attribute or object.

  • you know what’s worse, I changed the Messages and it’s in Portuguese, but the rendering of Lottie continues to bring in English

  • I did it! Thank you!!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.