如何使用php用新值更新json数据标签的值?

如何使用php用新值更新json数据标签的值?,php,json,Php,Json,我想用PHP更新标签值。假设我输入了相同的标签,那么它将匹配,如果存在任何相同的标签,那么它将更新该标签的值 [{ "label": "aa", "val": "12" }, { "label": "new", "val": "13" }, { "label": "yy", "val": "14" }] 在上面的data.json中,我想将标签“yy”的值更新为val:“20”。为此,我使用这个代码 $myFile = "data.json";

我想用PHP更新标签值。假设我输入了相同的标签,那么它将匹配,如果存在任何相同的标签,那么它将更新该标签的值

[{
    "label": "aa",
    "val": "12"
},
{
    "label": "new",
    "val": "13"
},
{
    "label": "yy",
    "val": "14"
}]
在上面的data.json中,我想将标签“yy”的值更新为val:“20”。为此,我使用这个代码

$myFile = "data.json";
$arr_data = array(); // create empty array
try
{
    //Get form data
    $formdata = array(
        'label'=> $_POST['label'],
        'val' => $_POST['val']
    );
    //Get data from existing json file
    $jsondata = file_get_contents($myFile);
    // converts json data into array
    $arr_data = json_decode($jsondata, true);
    foreach($arr_data as $item){
    }
    // array_push($arr_data,$formdata);
    $jsondata = json_encode($arr_data, JSON_PRETTY_PRINT);
    /*this is the matching label code*/
    if(in_array($formdata['label'],$item)){
        $item['val'] = $formdata['val'];
        echo $item['val'];
        echo "got it";
    }
    else echo "not matched";
    //write json data into data.json file
    if(file_put_contents($myFile, $jsondata)) {       
    }
    else 
        echo "error";
}
catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

将代码移动到
foreach
循环中匹配标签名称,以便可以比较json数组中的每个标签值。更新代码:

$myFile = "data.json";
try {
//Get form data
    $formdata = array(
        'label' => $_POST['label'],
        'val' => $_POST['val']
    );
    //Get data from existing json file
    $jsondata = file_get_contents($myFile);
   // converts json data into array
    $arr_data = json_decode($jsondata, true);
    foreach ($arr_data as $key => $item) {
        /* this is the matching label code */
        if ($formdata['label'] == $item["label"]) {
            $arr_data[$key]["val"] = $formdata['val'];
            echo $item['val'];
            echo "got it";
        } else
            echo "not matched";
    }
  //write json data into data.json file
    if (!file_put_contents($myFile, json_encode($arr_data, JSON_PRETTY_PRINT))) {
        echo "error";
    }
} catch (Exception $e) {
    echo 'Caught exception: ', $e->getMessage(), "\n";
}

您可以使用preg_replace。
Json的模式是非常可预测的。
使用regex意味着不需要对数组进行解码、编码甚至迭代

$label = "yy";
$val = 20;

echo preg_replace("/label\":\s*\"" . $label . "\",\s*\"val\":\s*\"\d+/",
                  "label\": \"" . $label . "\", \"val\": \"". $val, 
                  $json);


如果您需要知道标签是否已更新,则可以使用:

$label = "yy";
$val = 20;

$new = Preg_replace("/label\":\s*\"" . $label . "\",\s*\"val\":\s*\"\d+/",
                  "label\": \"" . $label . "\", \"val\": \"". $val, 
                  $json);
If($json != $new){
    Echo "label updated\n";
}else{
    Echo "nothing found\n";
}
Echo $new;

它将比较“旧”Json和新Json,如果它们不匹配,则标签已更新。

使用Json_decode()和Json_encode()的迭代解决方案

更新后


您需要正确缩进代码,这将有所帮助
$jsonData = '[
    {
        "label": "aa",
        "val": "12"
    },
    {
        "label": "new",
        "val": "13"
    },
    {
        "label": "yy",
        "val": "14"
    }
]';

echo '<strong>Before update</strong>';
echo '<pre>';
echo $jsonData;
echo '</pre>';

$jsonArray = json_decode($jsonData, true);


foreach ($jsonArray as $key => $jsonRow) {
    if (isset($jsonRow['label']) && $jsonRow['label'] == 'yy') {
        $jsonArray[$key]['val'] = 20;
    }
}

$jsonData = json_encode($jsonArray, JSON_PRETTY_PRINT);

echo '<strong>After update</strong>';
echo '<pre>';
echo $jsonData;
echo '</pre>';
[
    {
        "label": "aa",
        "val": "12"
    },
    {
        "label": "new",
        "val": "13"
    },
    {
        "label": "yy",
        "val": "14"
    }
]
[
    {
        "label": "aa",
        "val": "12"
    },
    {
        "label": "new",
        "val": "13"
    },
    {
        "label": "yy",
        "val": 20
    }
]