2015-11-08 88 views
0

我有下面的PHP代碼,它從另一個頁面(通過POST發送)接收一些變量。PHP - 更改JSON文件中的值

基於Ad_id和Name ...我試圖切換'激活'或'非激活'之間的狀態。

但是,當我執行此(使用正確的POST數據)...它似乎並沒有更新任何東西。我究竟做錯了什麼?

例如當我通過有效的廣告發送時,名稱爲& status ...然後去檢查JSON文件,它不會將「有效」切換爲「無效」。

PHP:

<?php 

// Get the post variables 
$ad_id = $_GET['ad']; 
$name = $_GET['name']; 
$status = $_GET['status']; 

// Get the JSON file 
$json = file_get_contents('test.json'); 

// Decode Json into an array 
$json = json_decode($json,true); 

// Within the array.. find the ad that matches the POST variable. 
foreach ($json['ads'] as $ad) { 
    if ($ad['id'] == $ad_id) { 

     // Within that Ad... find the candidate that matches the POST variable. 
     foreach ($ad['candidates'] as $candidate) { 
      if ($candidate['name'] == $name) { 

       // Within that candidate... check the value of 'status'. 
       if ($candidate['status'] == 'active') { 

        // If active, update the status to 'inactive'. 
        $candidate['status'] = 'inactive'; 

       } else { 

        // If inactive, update the status to 'active'. 
        $candidate['status'] = 'active'; 

       } 

      } 

     } 

    } 

} 

// Encode the array as JSON again 
$json = json_encode($json); 

// Save the JSON back to the server 
file_put_contents('test.json', $json, LOCK_EX); 

?> 

JSON:

{ 
    "ads": [ 
     { 
      "id": "12345678", 
      "hirername": "Demo Bar", 
      "candidates": [ 
       { 
        "status": "active", 
        "name": "Gregory Jones", 
        "dist": "Richmond (4km away)", 
        "exp1": "Barman at City Bar for 2 years", 
        "avail1": "Mon to Fri - Morning, Evening & Night", 
        "visa": "Australian Citizen", 
        "call": "0413451222" 
       }, 
       { 
        "status": "active", 
        "name": "Jackie Linton", 
        "dist": "Box Hill (13km away)", 
        "exp1": "Bar girl at Collins Bar for 1 year", 
        "avail1": "Mon to Fri - Morning & Evening", 
        "visa": "Working holiday visa", 
        "call": "0413456555" 
       } 
      ] 
     } 
    ] 
} 
+0

哪部分不工作? –

+0

當我通過有效的廣告,名稱和狀態發送...然後去檢查JSON文件,它不會將「活動」切換爲「不活動」。 –

+0

檢查答案。 –

回答

1

您應該通過鏈接使用foreach($items as &$item)語法迭代數據。
然後所有更改將在原始結構中。

現在,您將創建副本,同時迭代,修改副本,並且不會更改任何內容。

// Get the post variables 
$ad_id = $_GET['ad']; 
$name = $_GET['name']; 
$status = $_GET['status']; 

// Get the JSON file 
$json = file_get_contents('test.json'); 

// Decode Json into an array 
$json = json_decode($json,true); 

// Within the array.. find the ad that matches the POST variable. 
foreach ($json['ads'] as &$ad) { 
    if ($ad['id'] == $ad_id) { 

     // Within that Ad... find the candidate that matches the POST variable. 
     foreach ($ad['candidates'] as &$candidate) { 
      if ($candidate['name'] == $name) { 

       // Within that candidate... check the value of 'status'. 
       if ($candidate['status'] == 'active') { 

        // If active, update the status to 'inactive'. 
        $candidate['status'] = 'inactive'; 

       } else { 

        // If inactive, update the status to 'active'. 
        $candidate['status'] = 'active'; 
        } 

      } 

     } 

    } 

} 

// Encode the array as JSON again 
$json = json_encode($json); 

// Save the JSON back to the server 
file_put_contents('test.json', $json, LOCK_EX); 
0

你在你的代碼錯誤。你所做的閱讀和比較數值是正確的。

但是在寫入時,您還沒有使用$json變量的狀態。

您正在照原樣書寫。

if ($candidate['status'] == 'active') { 
    $candidate['status'] = 'inactive'; 
} else { 
    $candidate['status'] = 'active'; 
} 

上面你使用的變量爲$candidate

雖然寫入文件,

$json = json_encode($json); <------- $json is used. Where is $candidate?? 
$ret = file_put_contents('test.json', $json, LOCK_EX); 

編輯

$json = json_decode($json, true); 
$i = 0; 
$candidate1 = array(); 
// Within the array.. find the ad that matches the POST variable. 
foreach ($json['ads'] as $ad) { 
    if ($ad['id'] == $ad_id) { 
     $candidate1["id"] = $ad["id"]; 
     $candidate1["hirername"] = $ad["hirername"]; 

     // Within that Ad... find the candidate that matches the POST variable. 
     foreach ($ad['candidates'] as $candidate) { 
      if ($candidate['name'] == $name) { 
       $candidate1[$i] = $candidate; 
       $candidate['status'] == 'active' ? $candidate1[$i]['status'] = 'inactive' : $candidate1[$i]['status'] = 'active'; 
      } else { 
       $candidate1[$i] = $candidate; 
      } 
      $i++; 
     } 
    } 
} 

$json = json_encode($candidate1); 
$ret = file_put_contents('test.json', $json, LOCK_EX); 
+0

我想將整個$ json(包括我的更改)寫回test.json ...不只是$候選人。我需要以不同的方式寫下它嗎? –

+0

you shud將'$ json'的所有值都取回到'$ candidate',然後寫入您的文件。 –

+0

謝謝。我該怎麼做呢?你能舉個例子嗎? –

0
/** 
* Problem: 
* 
* - We have 2 variables 
* $adId: The id of the ad 
* $name: The name of the candidate 
* 
* - We want to update the status of the candidate depending the current status 
*/ 

// Fake JSON datas 
$json = '{ 

    "ads": [ 
    { 
     "id": "238", 
     "hirername": "Demo Bar", 
     "candidates": [ 
     { 
      "status": "active", 
      "name": "Gregory Jones" 
     }, 
     { 
      "status": "active", 
      "name": "Jackie Linton" 
     } 
     ] 
    }, 
    { 
     "id": "239", 
     "hirername": "Apple", 
     "candidates": [ 
     { 
      "status": "inactive", 
      "name": "Steve jobs" 
     } 
     ] 
    } 
    ] 

    }'; 

// Input variables 
$adId = 238; 
$name = 'Jackie Linton'; 

// Convert json as an array (we use true as a second param for that) 
$data = json_decode($json, true); 

// We fetch only the ads entity from the array 
$ads = $data['ads']; 

// Loop the ads 
foreach ($ads as $adIndex => $ad) 
{ 

    // Check if it's the right ad 
    if ($ad['id'] == $adId) 
    { 
    // Loop candidates 
    foreach ($ad['candidates'] as $candidateIndex => $candidate) 
    { 

     // Sanitize strings for a good check 
     $name = trim(strtolower($name)); 
     $candidateName = trim(strtolower($candidate['name'])); 

     // We found the right candidate 
     if ($name == $candidateName) 
     { 


     // Change status 
     if ($candidate['status'] == 'inactive') 
     { 
      $data['ads'][$adIndex]['candidates'][$candidateIndex]['status'] = 'active'; 
     } 
     else 
     { 
      $data['ads'][$adIndex]['candidates'][$candidateIndex]['status'] = 'inactive'; 
     } 

     } 

    } 

    } 

} 

// We changed the status. 
echo '<pre>'; 
var_dump($data); 
echo '</pre>'; 

與該代碼(測試工作),你就會明白你的問題: - 你保存數據在一個「本地」變量而不是你的全局JSON中。

+0

不錯!謝謝!現在我們已經更新了$ ads ...我們如何將這個返回到$ data或$ json中......然後執行一個file_put_contents將其放回到服務器上? –

+0

在我的示例中,變量$ data是您的JSON作爲狀態更新的數組。只需將其編碼爲JSON並保存即可);) –