0
I’m bringing you BD data that will be used as Values
of Keys
of my new file.json
that I’m trying to create. The Keys
are being read from an existing JSON file.
So, basically what I need to do is a file equal to the existing one, however, with different values. The format is more or less 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 is more or less like this:
{
"title1": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
},
"title2": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
},
}
With the loop shown above, I can already go through all the Keys
of each Title
(I don’t know the right name).
I would like help in logic to mount the new file with what I have now.
Show. Can you identify if the next iteration in the loop will exist or not ? Working with Json I couldn’t access any kind of
index
, only the key values.– PlayHardGoPro
@Playhardgopro in the loop I created the var
$keyTitle
concerning Title1, title2, etc and var$key
I created it to detect if it’s key1, Key2, etc., so you can know exactly what key it’s in and change it as needed, assuming you want to change thekey53
that is inside thetitle22
just make a if soif ($keyTitle == 'title22' && $key == 'key52') { $value = 'NOVO VALOR'; }
, then the trick is to use Ifs ;)– Guilherme Nascimento