Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
92 views
in Technique[技术] by (71.8m points)

PHP - How to change json values according to another json

I am new to PHP programming and I need your lights!

I have a JSON file like:

    "data": [{
            "DIAGNOSTIC_TYPE_ID": 1,
            "VALUE": "288.0"
        },
        {
            "DIAGNOSTIC_TYPE_ID": 2,
            "VALUE": "-0.1"
        },
        {
            "DIAGNOSTIC_TYPE_ID": 3,
            "VALUE": "327.67"
        },
        {
            "DIAGNOSTIC_TYPE_ID": 1,
            "VALUE": "288.0"
        },
        {
            "DIAGNOSTIC_TYPE_ID": 2,
            "VALUE": "-0.1"
        },
        {
            "DIAGNOSTIC_TYPE_ID": 3,
            "VALUE": "327.67"
        }]

and I have to change it using another json file which is like:

"diagnostics_keys": [
            {
                "ID": 1,
                "type": "BTTPV",
                "key": "0_193_bttpv",
                "unit": "V"
            },
            {
                "ID": 2,
                "type": "BTTPC",
                "key": "0_195_bttpc",
                "unit": "A"
            },
            {
                "ID": 3,
                "type": "AVGKMKWH",
                "key": "0_202_avgkmKwh",
                "unit": "Km/Kwh"
            }]

How can I combine these two (using the ID and type keys/values of the second json and replace the DIAGNOSTIC_TYPE_ID with those on first json)and take a result like the bellow json?

 "data": [{
                "DIAGNOSTIC_TYPE_ID": BTTPV,
                "VALUE": "288.0"
            },
            {
                "DIAGNOSTIC_TYPE_ID": BTTPC,
                "VALUE": "-0.1"
            },
            {
                "DIAGNOSTIC_TYPE_ID": AVGKMKWH,
                "VALUE": "327.67"
            },
            {
                "DIAGNOSTIC_TYPE_ID": BTTPV,
                "VALUE": "288.0"
            },
            {
                "DIAGNOSTIC_TYPE_ID": BTTPC,
                "VALUE": "-0.1"
            },
            {
                "DIAGNOSTIC_TYPE_ID": AVGKMKWH,
                "VALUE": "327.67"
            }]

Would anyone have any points or links that may know and may help?

question from:https://stackoverflow.com/questions/66066823/php-how-to-change-json-values-according-to-another-json

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
//change to arrays
$data = json_decode(YOUR_DATA_JSON, true); // I don't know your json variable names, so replace them
$diagnosticKeys = json_decode(YOUR_DIAGNOSTIC_KEYS_JSON, true);
//iterate over data, this is the one you want to change
foreach ($data as &$dataItem) {//(& is for replacing the values)
    //another foreach for diagnostic keys
    foreach ($diagnosticKeys as $diagnosticKey) {
        if ($dataItem["DIAGNOSTIC_TYPE_ID"] == $diagnosticKey["ID"] {
            $dataItem["DIAGNOSTIC_TYPE_ID"] = $diagnosticKey["type"];
        }
    }
}
//change to json again
$data = json_encode($data);

Not tested but should work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...