I have a json file that looks like this
{
"PL001": {
"player_name": "Player 1",
"player_email": "playeremail@email.com",
"hobbies": {
"SP001": {
"sport": "Soccer",
"positions": {
"FL1":{
"position": "Goalie"
}
}
}
}
},
"PL002": {
"player_name": "Player 2",
"player_email": "playeremail2@email.com",
"hobbies": {
"SP002": {
"sport": "Hockey",
"positions": {
"FL2":{
"position": "goaltender"
}
}
}
}
}
}
What I need to do is change my positions codes to my new ones so for example my FL2 will be PLFL2
I’ve managed to get to the positions code but I’m not sure as to how I should go about pushing the new code up without loosing data.
Here is my codes
$old_code = "FL2";
$new_code = "PLFL2";
$json = json_decode(file_get_contents(storage_path('/players.json')));
$result = [];
foreach ($json as $key => $value)
{
foreach($value->hobbies as $hobbiesCode => $hobby)
{
foreach ($hobby->positions as $positionCode => $position)
{
$positionCode = $new_code;
}
}
}
and that is where I get stuck. I’m not sure how to now go and update my json file with the new codes
Go to Source
Author: Aurilie