Creating JSON file with PHP

0

I'm bringing DB data that will be used as Values of Keys of my new file.json that I'm trying to create. The Keys is being read from an existing JSON file.

So, basically what I need to do is an existing file, but with different values. The format looks something like this:

 //Abro arquivo já existente
 $myFile  = fopen($fileName, "r") or die("Unable to open the file !");
 //Content possui o conteúdo do meu arquivo
 $content = json_decode(fread($myFile, filesize($fileName)));
 fclose($myFile); //Fecha arquivo

 foreach( $content as $keys => $value ) {         

    foreach( $value as $key) {
       //....
    }
 }

The format looks something like this:

{
    "title1": {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3"
     },
     "title2": {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3"      
     },
} 

With the loop shown above, I can already scroll through all Keys of each Title (I do not know correct name).

I would like help with logic to build the new file with what I have now.

    
asked by anonymous 06.04.2018 / 22:35

1 answer

1

You can use% com_commercial% in & (called reference), it should look like this:

<?php

$json = '{
    "title1": {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3"
    },
    "title2": {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3"      
    }
}';

$decodificado = json_decode($json);

if (!$decodificado) {
    die('JSON invalido');
}

//$title pega o title1 e title2, title3, etc
foreach ($decodificado as $keyTitle => &$title) {

    //$value pega o valor key1, key3, key3, key4, etc
    foreach ($title as $key => &$value) {

        //Aqui um exemplo para alterar as chaves com nome key2 e key3 apenas
        if ($key === 'key2') {
            $value = rand(0, 100);
        } elseif ($key === 'key3') {
            $value = rand(200, 300);
        }
    }
}

print_r($decodificado);

Example in IDEONE: link

Note that if you know exactly the location of the key you can simply set the object, for example, assuming you want to change it:

  • key2 inside title1
  • key1 inside title2
  • key3 inside title2

So you should do this:

<?php

$json = '{
    "title1": {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3"
    },
    "title2": {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3"      
    }
}';

$decodificado = json_decode($json);

if (!$decodificado) {
    die('JSON invalido');
}

$decodificado->title1->key2 = 'Novo valor FOO';
$decodificado->title2->key1 = 'Novo valor BAR';
$decodificado->title2->key3 = 'Novo valor BAZ';

print_r($decodificado);

Example in IDEONE: link

Then knowing the name of the keys you want to change then just use foreach and save again to a file (or wherever you want), for example:

...

$codificado = json_encode($decodificado);

file_put_contents('novo.json', $codificado);
    
06.04.2018 / 23:35