Prancingkiller
Prancingkiller

Reputation: 33

Edit json file with php

i have a json file structured this way:

{
   "data": {
       "Category": {
           "Product1": [{
               "Code": "abc"
           }],
           "Product2": [{
               "Code": "cba"
           }]
       },
       "Category2": {
           "Product3": [{
               "Code": "abcabc"
           }],
           "Product4": [{
               "Code": "cbacba"
           }]
       },
       "Category3": {
           "Product5": [{
               "Code": "abcabcabc"
           }],
           "Product6": [{
               "Code": "cbacbacba"
           }]
       }
   }
}
                            

How can i change the category names with a php script? for example if i have 2 variables received from a form, called "$oldcategory" (which will be one of the existing categories) and "$newcategory" how can it, open the json file, place "$newcategory" where "$oldcategory" is and re-save the file?

<?php
$oldcategory= $_POST['oldcategory'];
$newcategory= $_POST['newcategory'];
$jsonString = file_get_contents('data.json');
$data = json_decode($jsonString, true);

...
foreach($data["data"] as $key=>$value){
    $new = $data["data"][$oldcategory];
    $data["data"][$newcategory]= $new ; 
}
unset($data["data"][$oldcategory]);

$newJsonString = json_encode($data);
file_put_contents('data.json', $newJsonString);
?>

in case i was changing the "Category2" i'd like the result to be:

{
   "data": {
       "Category": {
           "Product1": [{
               "Code": "abc"
           }],
           "Product2": [{
               "Code": "cba"
           }]
       },
       "New_Category2": {
           "Product3": [{
               "Code": "abcabc"
           }],
           "Product4": [{
               "Code": "cbacba"
           }]
       },
       "Category3": {
           "Product5": [{
               "Code": "abcabcabc"
           }],
           "Product6": [{
               "Code": "cbacbacba"
           }]
       }
   }
}
                            

instead of this:

{
   "data": {
       "Category": {
           "Product1": [{
               "Code": "abc"
           }],
           "Product2": [{
               "Code": "cba"
           }]
       },
       "Category3": {
           "Product5": [{
               "Code": "abcabcabc"
           }],
           "Product6": [{
               "Code": "cbacbacba"
           }]
       },
       "New_Category2": {
           "Product3": [{
               "Code": "abcabc"
           }],
           "Product4": [{
               "Code": "cbacba"
           }]
       }
   }
}
                            

This was a possible solution:

foreach($data["data"] as $key=>$value){
    if ($key == $oldcategory){
    $key = $newcategory;}
    $new["data"][$key] = $value;
}


$newJsonString = json_encode($new);
file_put_contents('data.json', $newJsonString);

Upvotes: 1

Views: 118

Answers (2)

Prancingkiller
Prancingkiller

Reputation: 33

This ended up being the solution to my problem:

<?php
$oldcategory= $_POST['oldcategory'];
$newcategory= $_POST['newcategory'];
$jsonString = file_get_contents('data.json');
$data = json_decode($jsonString, true);

...
foreach($data["data"] as $key=>$value){
    if ($key == $oldcategory){
    $key = $newcategory;}
    $new["data"][$key] = $value;
}

$newJsonString = json_encode($new);
file_put_contents('data.json', $newJsonString);
?>

Upvotes: 2

Andrew Hardiman
Andrew Hardiman

Reputation: 969

You could use a conditional, and create a new array with the desired values. When you reach the $oldcategory in your loop, replace the key with the $newcategory.

$newArray = [];
foreach($data["data"] as $key=>$value){
    if($key == $oldcategory) {
        $newArray[$newcategory] = $value;
    } else {
        $newArray[$key] = $value;
    }
}

$newJsonString = json_encode($newArray);

Upvotes: 0

Related Questions