2014-12-07 58 views
0

最近即時通訊嘗試與我的高等教育項目通過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時,首先根據一些標準查找匹配條目,如nameuid,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"} 

不是先匹配現有條目,比廣告它的文件。

如果新的條目:(基於nameuidrdate

{"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 .....

感謝&問候

回答

1

下面是我如何去做(減去錯誤信息),將所有事情簡化爲一個函數。

define('JSON_FILE', 'file.json'); 

function addEntry($newEntry) 
{ 
    // flag to indicate whether an update was made or not; 
    // flag is by default set to false to assume no update was made 
    $update = false; 
    // retrieve list of current entries 
    if (file_exists(JSON_FILE)) { 
     $entries = json_decode(file_get_contents(JSON_FILE), true); 
    } 
    // if there are entries, look for a match 
    if (!empty($entries)) { 
     foreach ($entries as $i => $entry) { 
      // if match IS found... 
      if ($entry["Name"] == $newEntry["Name"] || 
       $entry["UID"] == $newEntry["UID"] || 
       $entry["RDate"] == $newEntry["RDate"]){ 
       // update/replace existing entry with new entry 
       $entries[$i] = $newEntry; 
       // turn on flag to indicate that an update was made 
       $update = true; 
       // no need to keep searching any further, so quit loop 
       break; 
      } 
     } 
    // otherwise if there no entries, create an empty list 
    } else { 
     $entries = array(); 
    } 
    // if no update was made i.e. no match was found, then it means a new entry needs to be added 
    if (!$update) { 
     // add new entry 
     $entries[] = $newEntry; 
    } 
    // save list of entries 
    file_put_contents(JSON_FILE, json_encode($entries)); 
} 

$entry = array(
    "Name" => $_POST["name"], 
    "UID" => $_POST["uid"], 
    "RDate" => $_POST["rdate"], 
    "ReLogin" => $_POST["relogin"], 
    "LTime" => $_POST["ltime"], 
    "Passive" => $_POST["passive"] 
); 

addEntry($entry); 
+0

謝謝@Mikey :)它的作品很棒。非常感謝老兄 – 2014-12-07 07:22:52

+0

我想知道一件事(它不是那個問題的一部分)。如果有人輸入條目的「value」來檢索整個條目,除非我們不知道提供的參數是「name」,「uid」還是「rdate」,否則我們如何匹配和檢索它?同樣的事情發生時,他想刪除基於提供的參數條目... – 2014-12-07 07:26:14

+0

對不起。我不太明白你剛剛提出的問題。 「除非我們不知道提供的參數是名稱或uid或rdata」,否則我們如何匹配和檢索它是什麼意思? – Mikey 2014-12-07 07:37:16

0

chkEntry很可能失敗,因爲json_decode()的錯誤了。 Json_decode在錯誤時返回null,並且在php $x = null; $x[1] === NULL中始終爲真; null $ x上的所有下標值都將爲null,因此彼此相等。