Suraj Palwe
Suraj Palwe

Reputation: 2120

Unable to update json file in PHP

I have json file which has the following structure of json object

{
  "Project_3": {
      "link": "",
      "title": "",
      "desc": ""
  },
  "Project_2": {
      "link": "",
      "title": "",
      "desc": ""
  },
  "Project_1": {
      "link": "",
      "title": "",
      "desc": ""
  }
}

Now what I want is to update this json file as like this

Project_3 = Project_2

Project_2 = Project_1

I mean the contents of Projects_3 should be as that of Projects_2 and for Project_2 as that of Project_1 and for Project_1 should be the next input from the form data

Uptil now this is what I have tried in php.

$projects = json_decode(file_get_contents('../json/recent_projects.json','w'));

$projects->Project_3 = $projects->Project_2;
$projects->Project_2 = $projects->Project_1;

$projects->Project_1->link  = htmlspecialchars($_POST['project_link']);
$projects->Project_1->title = htmlspecialchars($_POST['project_name']);
$projects->Project_1->desc  = htmlspecialchars($_POST['project_desc']);

$fh = fopen("../json/recent_projects.json", 'w') or die('File cannot be opened');
fwrite($fh, json_encode($projects,JSON_UNESCAPED_UNICODE));
fclose($fh);

But what happens is the content of Project_3 and Project_2 remains the same always and only Project_1 gets updated. I don't understand why this is happening.

Edit

I got the answer to my question but why wan unset used in the process. That I didn't got!Any help or suggestions are warmly welcomed!

Upvotes: 0

Views: 209

Answers (1)

PHPhil
PHPhil

Reputation: 1540

Tested your code, but here Project_3 and Project_2 did change. Only error I got is that both Project_2 and Project_1 got updated by the $_POST variables. So I just unset() Project_1. Now its working like you want it to.

<?php 

$json = '{
  "Project_3": {
      "link": "L3",
      "title": "T3",
      "desc": "D3"
  },
  "Project_2": {
      "link": "L2",
      "title": "T2",
      "desc": "D2"
  },
  "Project_1": {
      "link": "L1",
      "title": "T1",
      "desc": "D1"
  }
}';


$projects = json_decode($json);

echo "<pre>";
print_r($projects);

$projects->Project_3 = $projects->Project_2;
$projects->Project_2 = $projects->Project_1;

unset($projects->Project_1);


$projects->Project_1->link  = htmlspecialchars($_POST['project_link']);
$projects->Project_1->title = htmlspecialchars($_POST['project_name']);
$projects->Project_1->desc  = htmlspecialchars($_POST['project_desc']);

echo "<pre>";
print_r($projects);

$fh = fopen("../json/recent_projects.json", 'w') or die('File cannot be opened');
fwrite($fh, json_encode($projects,JSON_UNESCAPED_UNICODE));
fclose($fh);

?>

Output:

stdClass Object
(
    [Project_3] => stdClass Object
        (
            [link] => L3
            [title] => T3
            [desc] => D3
        )

    [Project_2] => stdClass Object
        (
            [link] => L2
            [title] => T2
            [desc] => D2
        )

    [Project_1] => stdClass Object
        (
            [link] => L1
            [title] => T1
            [desc] => D1
        )

)

stdClass Object
(
    [Project_3] => stdClass Object
        (
            [link] => L2
            [title] => T2
            [desc] => D2
        )

    [Project_2] => stdClass Object
        (
            [link] => L1
            [title] => T1
            [desc] => D1
        )

    [Project_1] => stdClass Object
        (
            [link] => L5
            [title] => T5
            [desc] => D5
        )

)

Upvotes: 1

Related Questions