取消设置数组项并更新json对象

时间:2016-11-04 21:24:07

标签: php json

我有下面的json,我想要的是从它取消设置数组值并更新json,就像“pstsg”中的数组有“min”= 0将取消设置但不更新json

"g": {
    "mid": 1478144134757,
    "gid": "0021600062",
    "gdte": "2016-11-02",
    "htm": "2016-11-02T19:00:00",
    "vtm": "2016-11-02T20:00:00",
    "etm": "2016-11-02T21:00:00",
    "gdtutc": "2016-11-03",
    "utctm": "01:00",
    "ac": "Salt Lake City",
    "as": "UT",
    "gcode": "20161102/DALUTA",
    "next": "http://abcd.json",
    "ar": 0,
    "p": 4,
    "st": 3,
    "stt": "Final",
    "cl": "00:00.0",
    "lpla": {
        "evt": 493,
        "cl": "00:00.0",
        "de": "End Period",
        "locX": 0,
        "locY": -80,
        "opt1": 0,
        "opt2": 0,
        "mtype": 0,
        "etype": 13,
        "opid": "",
        "tid": 0,
        "pid": 0,
        "hs": 97,
        "vs": 81,
        "epid": "",
        "oftid": 1610612762
    },
    "vls": {
        "q1": 14,
        "s": 81,
        "ftout": 0,
        "stout": 1,
        "ta": "DAL",
        "tstsg": {
            "fga": 77,
            "fgm": 33,
            "tpa": 26,
            "tpm": 7,
            "fta": 10,
            "ftm": 8,
            "oreb": 6,
            "dreb": 30,
            "reb": 36,
            "ast": 18,
            "stl": 4,
            "blk": 4,
            "pf": 20,
            "tov": 12,
            "fbpts": 0,
            "fbptsa": 1,
            "fbptsm": 0,
            "pip": 28,
            "pipa": 27,
            "pipm": 14,
            "ble": 1,
            "bpts": 32,
            "tf": 2,
            "scp": 0
        },
        "pstsg": [
            {
                "fn": "Harrison",
                "ln": "Barnes",
                "num": "40",
                "pos": "F",
                "min": 35,
                "sec": 42,
                "totsec": 2142,
                "fga": 13,
                "fgm": 6,
                "tpa": 3,
                "tpm": 0,
                "fta": 2,
                "ftm": 2,
                "oreb": 0,
                "dreb": 2,
                "reb": 2,
                "ast": 1,
                "stl": 0,
                "blk": 1,
                "pf": 1,
                "pts": 14,
                "tov": 1,
                "fbpts": 0,
                "fbptsa": 0,
                "fbptsm": 0,
                "pip": 0,
                "pipa": 1,
                "pipm": 0,
                "court": 0,
                "pid": 203084,
                "pm": -13,
                "blka": 0,
                "tf": 0,
                "status": "A"
            },
            {
                "fn": "Dirk",
                "ln": "Nowitzki",
                "num": "41",
                "pos": "F",
                "min": 0,
                "sec": 48,
                "totsec": 1728,
                "fga": 14,
                "fgm": 4,
                "tpa": 5,
                "tpm": 0,
                "fta": 1,
                "ftm": 1,
                "oreb": 1,
                "dreb": 4,
                "reb": 5,
                "ast": 0,
                "stl": 1,
                "blk": 0,
                "pf": 3,
                "pts": 9,
                "tov": 1,
                "fbpts": 0,
                "fbptsa": 0,
                "fbptsm": 0,
                "pip": 8,
                "pipa": 5,
                "pipm": 4,
                "court": 0,
                "pid": 1717,
                "pm": -20,
                "blka": 1,
                "tf": 0,
                "status": "A"
            },

我想要的是删除未设置的数组值并更新json对象。

foreach ($gdUrl_data as $key => $value) {
    $vpstgcount = count($value["vls"]["pstsg"]);
    for ($j = 0; $j <$vpstgcount; $j++) {
        $minVal = $value["vls"]["pstsg"][$j]["min"];
        if ($minVal == 0)
        {
            unset($value["vls"]["pstsg"][$j]);
        }
    }
  }
    $gdUrl_data = json_encode($gdUrl_data);

这将取消设置数组但不更新json。

2 个答案:

答案 0 :(得分:0)

function unsetKey($json, $key) {
    $json = json_decode($json, true);
    unset($json[$key]);
    return json_encode($json);
}

答案 1 :(得分:0)

$value是数组元素的副本,因此当您取消它的一部分时,您不会影响原始数组。如果您希望它是实际的数组元素,请使用引用变量:

foreach ($gdUrl_data as &$value) {
    foreach ($value["vls"]["pstg"] as $j => $el) {
        if ($el["min"] == 0) {
            unset($value[$j]);
        }
    }
}