简体   繁体   English

在php中以JSON格式存储表单数据时定义多维数组

[英]Defining multidimensional array while storing form data in JSON format in php

I have to convert form data to JSON format. 我必须将表单数据转换为JSON格式。 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. 这就是我的JSON的样子。 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: 要获取例如ip_address您可以执行以下操作:

$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. 这会将您的json string解码为多维数组,然后您可以简单地对其进行回显。

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(); 比用$_POST["something"]值创建数组,然后使用json_encode();数组编码为json 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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