最近即時通訊嘗試與我的高等教育項目通過PHP的JSON數據。但是,我在管理/更新JSON文件時掛機。PHP:在更新json文件時,首先匹配條目。如果匹配,比替換/更新,否則寫
讓我介紹你的整個場景:
它的一款入門基礎的項目,所有項目必須在JSON文件(.json
)來保存,格式是:
[{"Name":"xyz","UID":"1234","RDate":"11122014","ReLogin":"true","LTime":"1e3","Passive":"false"},{"Name":"abc","UID":"5678","RDate":"01102014","ReLogin":"true","LTime":"1e3","Passive":"false"},......]
現在,我必須做的是:當一個新條目發送給PHP時,首先根據一些標準查找匹配條目,如name
,uid
,rdate
。 如果匹配成功,比替換/更新文件中的整個條目 如果匹配未找到,則將新條目添加/寫入文件。
要做到這一點,我目前正試圖用這些一串代碼:
1)`函數chkEntry($ UID,$輸入)
$file='file.json';
$s[]=$entry;$match=0;
global $RESP;
if(0==filesize($file)){
return true;
}else{
$data=json_decode(file_get_contents($file),true);
foreach($data as $keyD=>$valD){
foreach($s as $keyS=>$valS){
if($valD["Name"]!=$valS["Name"]||$valD["UID"]!=$valS["UID"]||$valD["RDate"]!=$valS["RDate"]){ // here `RDate` is always unique, so I can use it for the matching process
$match=0;
}else{
$match=1;
}
}
}
if($match==1){return false;}else{return true;}
}
這chkEntry
總是返回false :( 同時,我不知道如何更換INT該文件的同時進入,如果發現匹配
修訂chkEntry function
:匹配標準僅限於單僅參數[uid
]。如果找到匹配項,則刪除整個條目並將return true
設置爲addEntry function
,以便addEntry
添加新條目。但不幸的是,這個function
也未按預期工作。
function chkEntry($uid,$entry){
$file='file.json';
$data=json_decode(file_get_contents($file),true);
foreach($data as $keyD=>$valD){
if($valD["uid"]==$entry["uid"]){
unset($data[$keyD]); //delete the entry, if matched
file_put_contents($file,json_encode($data));
return true;
}
}
return true;
}
2)`函數的addEntry($ UID,$輸入)
global $RESP;
$ready=false;
$file='file.json';
if(addFile($uid)){ //`function addFile`, if file not exists, than create an blank file
$data=json_decode('file.json',true);
unset($file);
if(chkEntry($uid,$entry)){ //`function chkEntry`, match the entry
$data[]=$entry;
file_put_contents('file.json',json_encode($data));
unset($data);
}else{
$RESP["entry"]="This entry is already exists into Database";
}
$ready=true;
}else{
$RESP["err"]="We are unable to add the entry right now. Please try after some time!";
}
if($ready){
if(!isset($RESP["entry"])){$RESP["entry"]="for evaluatuion";}
return true;
}else{
$RESP["err"]="Something went wrong! Please try to add this later.";
return false;
}
3)
$entry=array("Name"=>$_POST["name"],"UID"=>$_POST["uid"],"RDate"=>$_POST["rdate"],"ReLogin"=>$_POST["relogin"],"LTime"=>$_POST["ltime"],"Passive"=>$_POST["passive"]);
addServer($uid,$entry);
所以,我的問題是:
如何發現/匹配功能應該進行管理,使其return true
如果匹配發現,否則return false
和IF MATCH FOUND,比如何重新將整個條目放入/更新到文件中。
例如:如果新條目
{"Name":"try","UID":"1111","RDate":"12122014","ReLogin":"`true","LTime":"1e3","Passive":"false"}
不是先匹配現有條目,比廣告它的文件。
如果新的條目:(基於name
,uid
,rdate
)
{"Name":"xyz","UID":"2222","RDate":"15122014","ReLogin":"true","LTime":"1e3","Passive":"false"} //match found, based on `name`
OR
{"Name":"nws","UID":"1234","RDate":"19122014","ReLogin":"true","LTime":"1e3","Passive":"false"} //match found, based on `uid`
OR
{"Name":"nws","UID":"2222","RDate":"11122014","ReLogin":"true","LTime":"1e3","Passive":"false"} //match found, based on `rdate`
不是更新/更換舊條目{"Name":"xyz","UID":"1234","RDate":"11122014","ReLogin":"true","LTime":"1e3","Passive":"false"}
用它。
幫我PLZ .....
感謝&問候
謝謝@Mikey :)它的作品很棒。非常感謝老兄 – 2014-12-07 07:22:52
我想知道一件事(它不是那個問題的一部分)。如果有人輸入條目的「value」來檢索整個條目,除非我們不知道提供的參數是「name」,「uid」還是「rdate」,否則我們如何匹配和檢索它?同樣的事情發生時,他想刪除基於提供的參數條目... – 2014-12-07 07:26:14
對不起。我不太明白你剛剛提出的問題。 「除非我們不知道提供的參數是名稱或uid或rdata」,否則我們如何匹配和檢索它是什麼意思? – Mikey 2014-12-07 07:37:16