2014-04-22 91 views
0

這是我的問題。我有一個JSON文件是這樣的:用PHP添加新的JSON對象到數組中

[ 
{ 
    "projectName": "test", 
    "clientName": "test2", 
    "dateValid": "2014-04-18", 
    "account": { 
     "accountAmount": null, 
     "accountDate": "2014-04-19", 
     "accountType": null 
    }, 
    "total": { 
     "totalAmount": null, 
     "totalDate": "2014-04-18", 
     "totalType": null 
    } 
}] 

而且我想PHP打開此文件,並添加另一個對象,所以我的文件將是這樣的:

[ 
    { 
     "projectName": "test", 
     "clientName": "test2", 
     "dateValid": "2014-04-18", 
     "account": { 
      "accountAmount": null, 
      "accountDate": "2014-04-19", 
      "accountType": null 
     }, 
     "total": { 
      "totalAmount": null, 
      "totalDate": "2014-04-18", 
      "totalType": null 
     } 
    }, 
    { 
     "projectName": "test", 
     "clientName": "test2", 
     "dateValid": "2014-04-18", 
     "account": { 
      "accountAmount": null, 
      "accountDate": "2014-04-19", 
      "accountType": null 
     }, 
     "total": { 
      "totalAmount": null, 
      "totalDate": "2014-04-18", 
      "totalType": null 
     } 
    } 
] 

它應該是很簡單的,但我無法做到這一點。我嘗試了多種方式來做到這一點:

$file = 'base.json'; 
if(file_exists ($file)){ 
    echo 'base.json found'; 
    $fileContent = file_get_contents($file); 
    $oldData = json_decode($fileContent, true); 
    echo var_export($oldData); 
} 
else { 
    echo 'base.json not found'; 
    $oldData = []; 
} 


echo $data; 
$data = json_encode($data); 
$oldData = json_encode($oldData); 
echo $data; // debug 
file_put_contents('base.json', '['.$data.','.$oldData.']'); 

是的,我放了很多回聲來調試數據過程...我缺少什麼?

+0

[PHP jsonencode到一個文件,格式問題(http://stackoverflow.com/questions/21492338/php-jsonencode-to-a-file-format-issue) – Barmar

+0

該主題的可能重複/它的答案不是我要找的。 – enguerranws

+0

下面給出的答案基本上與您接受的答案相同。 – Barmar

回答

2

新數據添加到與陣列:

$oldData[] = $data; 

然後把它寫回到檔案:

file_put_contents('base.json', json_encode($oldData)); 
+0

感謝這工作了很多,我不知道這個語法:$ array [] = $ newStuff; – enguerranws

+0

它相當於'array_push($ oldData,$ data)' – Barmar

5

你正在對待這個像字符串操作,這是去錯誤的方式。你需要結合這兩個結構,而他們是對象,之前你重新編碼爲JSON。

這三行......

$data = json_encode($data); 
$oldData = json_encode($oldData); 
file_put_contents('base.json', '['.$data.','.$oldData.']'); 

應該被改寫成...

// Create a new array with the new data, and the first element from the old data 
$newData = array($data, $oldData[0]); 
$newData = json_encode($newData); 
file_put_contents('base.json', $newData); 
+0

謝謝你的解釋。還有一個問題:$ oldData [0]選擇數組中的第一個對象,當我想選擇所有對象時(我的例子不清楚)。我如何選擇$ oldData中的所有對象? – enguerranws

1

試試這個

$arr = json_decode(file_get_contents('myFile.json')); 

// append a new "object" (array) 
$arr[] = array(
    "projectName" => "test", 
    "clientName" => "test2", 
    "dateValid" => "2014-04-18", 
    "account" => array(
     "accountAmount" => null, 
     "accountDate" => "2014-04-19", 
     "accountType" => null 
    ), 
    "total" => array(
     "totalAmount" => null, 
     "totalDate" => "2014-04-18", 
     "totalType" => null 
    ) 
); 

$json = json_encode($arr); 

file_put_contents('myFile.json', $json); 
0

試試這個:

<?php 


$json = '[ 
{ 
    "projectName": "test", 
    "clientName": "test2", 
    "dateValid": "2014-04-18", 
    "account": { 
     "accountAmount": null, 
     "accountDate": "2014-04-19", 
     "accountType": null 
    }, 
    "total": { 
     "totalAmount": null, 
     "totalDate": "2014-04-18", 
     "totalType": null 
    } 
}]'; 

$json_to_add=' { 
     "projectName": "test", 
     "clientName": "test2", 
     "dateValid": "2014-04-18", 
     "account": { 
      "accountAmount": null, 
      "accountDate": "2014-04-19", 
      "accountType": null 
     }, 
     "total": { 
      "totalAmount": null, 
      "totalDate": "2014-04-18", 
      "totalType": null 
     } 
    }'; 

$data = json_decode($json); 
$data_to_add = json_decode($json_to_add); 
$data[]=$data_to_add ; 
var_dump($data); 
2

您可以將您的JSON變種到數組specifing第二PARAM爲true json_decode和使用後array_merge以包括新的數組var和轉換再次json。

<?php 
$json1 = '[{ 
    "projectName": "test", 
    "clientName": "test2", 
    "dateValid": "2014-04-18", 
    "account": { 
     "accountAmount": null, 
     "accountDate": "2014-04-19", 
     "accountType": null 
    }, 
    "total": { 
     "totalAmount": null, 
     "totalDate": "2014-04-18", 
     "totalType": null 
    } 
}]'; 

$json2 = '[{ 
    "projectName": "test 2", 
    "clientName": "test3", 
    "dateValid": "2014-04-22", 
    "account": { 
     "accountAmount": null, 
     "accountDate": "2014-04-27", 
     "accountType": null 
    }, 
    "total": { 
     "totalAmount": null, 
     "totalDate": "2014-04-27", 
     "totalType": null 
    } 
}]'; 

$arr1 = json_decode($json1, true); 
$arr2 = json_decode($json2, true); 

$json2 = json_encode(array_merge($arr1, $arr2)); 

?>