1

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);
15
  • 1
    Loop the array, find the value, replace it? Have you attempted anything? If so, please share your effort and explain where and how it fails to do what you want. Commented Apr 16, 2021 at 13:51
  • i tried this: foreach($data["data"] as $key=>$value){ if ($key == $oldcategory) { $key = $newcategory; } } Commented Apr 16, 2021 at 13:56
  • Add it to the question using the "edit" button, please. But that looks like it covers trying to list the data...did it identify the record as you wanted? All you have to do then is remove / rename that element. is that the bit your're stuck on? Commented Apr 16, 2021 at 13:57
  • 1
    When you do $key = $newcategory;, you're merely reassigning a temporary variable where the loop stores the key of the current element. To modify the array, you would need to assign a new value to it under that key: $data['data'][$key] = $newcategory. Commented Apr 16, 2021 at 14:00
  • 1
    i've updated the question to be more clear, i'm gonna look at your suggestions thanks! Commented Apr 16, 2021 at 14:38

2 Answers 2

2

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);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

I believe you may have posted the wrong code. The solution you gave, that you have embedded in your question, includes an if statement.
0

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);

2 Comments

i think we ended up with a similar solution? check the last part of the question that i updated while you were typing the answer
@Prancingkiller yes, they effectively the same, I might like your solution even more, you should post it as an answer here also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.