0
を削除し、私はデータを以下にJSONファイルを持っている:PHP - JSONオブジェクトから値によって
{
"0": {
"name": "Test1",
"devices": [
{
"name": "Test1",
"code": "654345",
"state": "on"
},
{
"name": "Test2",
"code": "876543",
"state": "off"
},
{
"name": "Test3",
"code": "44444",
"state": "off"
}
]
},
"1": {
"name": "Test2",
"devices": [
{
"name": "Test4",
"code": "987654",
"state": "on"
}
]
}
}
今、私はデバイスのいずれかの設定を解除したいです。私は、PHPのコードを次でそれを試してみたが、それが動作しない理由を把握することはできません。すべての助けを事前に
$file = '../data/data.json';
$json_data = file_get_contents($file);
$json = json_decode($json_data);
foreach ($json as $key => $value) {
foreach ($value->devices as $k => $v) {
if ($v->name == $_POST["name"]) {
unset($json[$key]);
}
}
}
file_put_contents($file, json_encode($json));
感謝を!
設定を解除しようとしているキーは何ですか? '$ _POST ['name']'の値は何ですか? – Machavity
$ _POST ['name']はデバイスの名前です。 –