2015-07-03 83 views
0

我有具有JSON對象的結構如下無法更新JSON文件中的PHP

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

現在,我要的是更新這個JSON文件作爲這樣

Project_3 = JSON文件Project_2

Project_2 = PROJECT_1

我指的是CON Projects_3的帳篷應該與Project_2和Project_2的帳戶一樣,Project_1和Project_1應該是從表單數據的下一個輸入

Uptil現在這是我嘗試過的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); 

但是,什麼情況是Project_3的內容和Project_2始終保持不變,僅PROJECT_1得到更新。我不明白爲什麼會發生這種情況。

編輯

我得到的回答我的問題,但爲什麼婉取消設置過程中使用。我沒有得到!任何幫助或建議都熱烈歡迎!

回答

1

測試了你的代碼,但在這裏Project_3和Project_2確實發生了變化。只有我得到的錯誤是,Project_2和Project_1都通過$_POST變量進行了更新。所以我只是unset() Project_1。現在它的工作就像你想要的一樣。

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

?> 

輸出:

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 
     ) 

) 
+0

感謝您的幫助。我可以獲得更多信息,爲什麼您使用未設置,以便根據需要進行更改! –

+1

說實話,我真的不知道爲什麼'unset()'是需要的。我認爲Project_1和Project_2受3'$ _POST'變化影響,因爲Project_2 = Project_1。通過取消設置項目1,他們不再連接。它只是一個猜測>但如果你真的想知道爲什麼我會建議問一個新的問題。 – PHPhil