0
{
    "general": {
        "knx_Gateway": "te",
        "knx_Port": "gas",
        "knx_Medium": "TP1",
        "knx_timezone": "+3"
    },
    "devices":[
      {
        "id": "0",
        "knx_dn": "testing",
        "knx_ga": "2",
        "knx_dt": "Bit"
      },
      {
        "id": "1",
        "knx_dn": "0",
        "knx_ga": "0",
        "knx_dt": "Bit"
      },
      {
        "id": "2",
        "knx_dn": "0",
        "knx_ga": "5",
        "knx_dt": "Byte"
      },
      {
        "id": "3",
        "knx_dn": "0",
        "knx_ga": "0",
        "knx_dt": "Byte"
      },
      {
        "id": "4",
        "knx_dn": "0",
        "knx_ga": "0",
        "knx_dt": "Byte"
      }
  ]
}

This is what i tried so far:

//getting the data from the HTML page into an JSON format
$knxGenSet->general->knx_Gateway = $_POST["ipgate"];
$knxGenSet->general->knx_Port = $_POST["ipport"];
$knxGenSet->general->knx_Medium = $_POST["ipmedium"];
$knxGenSet->general->knx_timezone = $_POST["iptimezone"];

for($i=0; $i<5; $i++){
    echo "The number is " . $i . "<br>";

    $knxGenSet->devices->id = $i;
    $knxGenSet->devices->knx_ga = $_POST["dn" + $i];
    $knxGenSet->devices->knx_dn = $_POST["ga" + $i];
    $knxGenSet->devices->knx_dt = $_POST["dt" + $i];
}

I cant get the other part with devices right it seems that I am doing something wrong here btw i can allready read this json format .. the reading part works perfectly! :)

Thanks in advance (kind of newb on json territory! :()

MaartenDev
  • 5,631
  • 5
  • 21
  • 33
Bac Babc
  • 25
  • 5
  • 1
    What is the current output? Do any errors occur? – MaartenDev Feb 15 '20 at 14:20
  • 1
    Does this answer your question? [Preferred method to store PHP arrays (json\_encode vs serialize)](https://stackoverflow.com/questions/804045/preferred-method-to-store-php-arrays-json-encode-vs-serialize) – Rhalp Darren Cabrera Feb 15 '20 at 14:43

1 Answers1

1

The format can be specified with an object:

<?php
$_POST = [
    "ipgate" => "testing",
    "ipport" => "testing",
    "ipmedium" => "testing",
    "iptimezone" => "testing",
    "dn0" => "test_1",
    "ga0" => "test_1",
    "dt0" => "test_1",
    "dn1" => "test_2",
    "ga1" => "test_2",
    "dt1" => "test_2",
];

$knxGenSet = (object) [
    'general' => (object) [],
    'devices' => []
];

$knxGenSet->general->knx_Gateway = $_POST["ipgate"];
$knxGenSet->general->knx_Port = $_POST["ipport"];
$knxGenSet->general->knx_Medium = $_POST["ipmedium"];
$knxGenSet->general->knx_timezone = $_POST["iptimezone"];

for($i=0; $i<2; $i++){
  //  echo "The number is " . $i . "<br>";

    $knxGenSet->devices[] = (object) [
        'id' => $i,
        'knx_ga' => $_POST["dn" . $i],
        'knx_dn' => $_POST["ga" . $i],
        'knx_dt' => $_POST["dt" . $i]
    ];
}

echo json_encode($knxGenSet);

result:

{
  "general": {
    "knx_Gateway": "testing",
    "knx_Port": "testing",
    "knx_Medium": "testing",
    "knx_timezone": "testing"
  },
  "devices": [
    {
      "id": 0,
      "knx_ga": "test_1",
      "knx_dn": "test_1",
      "knx_dt": "test_1"
    },
    {
      "id": 1,
      "knx_ga": "test_2",
      "knx_dn": "test_2",
      "knx_dt": "test_2"
    }
  ]
}

MaartenDev
  • 5,631
  • 5
  • 21
  • 33