duanchu0031 2015-11-08 05:22 采纳率: 0%
浏览 132
已采纳

PHP - 更改JSON文件中的值

I have the below php code that receives some variables from another page (sent via POST).

Based on the Ad_id and Name... i'm trying to toggle the status between 'active or 'inactive'.

However when i execute this (with correct POST data)... it doesn't seem to update anything. What am i doing wrong?

e.g. When i send through a valid ad, name & status... then go and check the JSON file, it asn't toggled 'active' to 'inactive'.

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"
                }
            ]
        }
    ]
}
  • 写回答

3条回答 默认 最新

  • duanjia1870 2015-11-08 07:17
    关注

    You should iterate your data by links with foreach($items as &$item) syntax.
    Then all changes will be in the original structure.

    Now, you create copies, while iterate, modify copies, and nothing changed.

    // 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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?