使用数组值作为其他数组索引。非法抵消类型

时间:2017-09-11 08:57:52

标签: php

我的JSON文件中有3个数组

{
    "Temperatures": [26.00, 26.50, 26.50, 0.00, 0.00, 20.00, 0.00, 0.00, 0.00],
    "ThermoStatus": [0, 0, 0, -6, -6, -6, -3, -6, -6],
    "FurnaceArray": [{
            "Oven": {
                "Thermo": [1, 4],
                "SetPoint": 60.5
            },
            "Part": {
                "Thermo": [2, 3],
                "SetPoint": 60.5
            },
            "Outer": {
                "Thermo": [5],
                "SetPoint": 60.5
            },
            "TimerElapsed": 79,
            "HeaterOutput": 0.600,
            "ProgramPhase": 1,
            "HeaterState": 3,
            "OvenStatus": {
                "DoorOpened": true,
                "EmergencyPressed": false,
                "ProgramDone": false
            }
        },
        {
            "Oven": {
                "Thermo": [5, 6],
                "SetPoint": 60.5
            },
            "Part": {
                "Thermo": [7],
                "SetPoint": 60.5
            },
            "Outer": {
                "Thermo": [5],
                "SetPoint": 60.5
            },
            "TimerElapsed": 79,
            "HeaterOutput": 0.600,
            "ProgramPhase": 1,
            "HeaterState": 3,
            "OvenStatus": {
                "DoorOpened": true,
                "EmergencyPressed": false,
                "ProgramDone": false
            }
        }
    ]
}

我将Oven Thermo和Part Thermo结合起来。像这样。

$comTemperatures = array($json->FurnaceArray[0]->Oven->Thermo , $json->FurnaceArray[0]->Part->Thermo);

现在我想使用$ comTemperatures作为Thermostatus的索引。如何在$ comTemperatures

中显示指数的温度

我现在有了这个

foreach ($comTemperatures as $value) {
    if ($thermostatus[$value] == 0) {
        echo $temperatures[$value];
    }
}

但我在foreachloop收到错误“非法偏移类型”。 有人可以帮助我

1 个答案:

答案 0 :(得分:1)

如果我理解正确的话:

$jsonArray = json_decode($jsonString); //$jsonString is the JSON you shared

$thermostatus = $jsonArray->ThermoStatus;
$temperatures = $jsonArray->Temperatures;
//Merge two arrays into one instead of making one array of arrays 
$comTemperatures = array_merge($json->FurnaceArray[0]->Oven->Thermo , $json->FurnaceArray[0]->Part->Thermo);     
foreach ($comTemperatures as $value) {
   if ($thermostatus[$value] == 0) {
        echo $temperatures[$value];
    }
}

示例:https://eval.in/859257