简体   繁体   中英

Defining multidimensional array while storing form data in JSON format in php

I have to convert form data to JSON format. I am trying to achieve this:-

{"appConfiguration" : {

    "configuration_name" = "AS400 Configuration",
    "configuration_version" = "1.001",
    "connection" : [ {
        "ip_address" : [    “10.10.10.01”,
                            “10.10.10.02”,
                            “10.10.10.03”
                            // all saved IP Address.
                        ]
        "port" : "23"
        "ssl" : "NO",
        "device_name" : "Agicent Device",
        "name" : "Puga",
        "user" : "smart gladiator",
        "password" : "sgl2013",
        "barcode_enter" : "NO",]}}

This is what my JSON should look like. I am able to store data in single-dimension array; how do I create a structure like this?

"connection":["ohiuh","ghu","ip_address":["something","something","something"]]

To get for example ip_address you can do this:

$array = json_decode($jsonstring);

echo $array['connection']['ip_address']['something'];

This will decode your json string into an multidimensional array and than you can simply echo it.

To encode it:

$test = array("appConfiguration" => array("configuration_name"=> "AS400 Configuration", "configuration_version" => "1.001", "connection"=> array("ip_address" => array('10.10.10.01', '10.10.10.02', '10.10.10.03'), "port" => "23",
                                             "ssl" => "NO",
                                             "device_name" => "Agicent Device",
                                             "name" => "Puga",
                                             "user" => "smart gladiator",
                                             "password" => "sgl2013",
                                             "barcode_enter" => "NO")));
echo(json_encode($test));

To use data you get from a form you can do this:

$array = array('connection'=>array($_POST["ohiuh"],$_POST["ghu"] , array("ip_address"=>array($_POST["ip_adress1"],$_POST["ip_adress2"],$_POST["ip_adress3"])))));
echo json_encode($array);

Write a form with the value you need and than post them. Than create the array with the $_POST["something"] values and encode this array to json with json_encode();

Hope this is now the answer to your question.

Try with this

$arr = array('connection'=>array("ohiuh","ghu" , json_encode(array("ip_address"=>array("something","something","something")))));
echo json_encode($arr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM