2015-11-08 23 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并保存即可);) –