【问题标题】:Defining multidimensional array while storing form data in JSON format in php在php中以JSON格式存储表单数据时定义多维数组
【发布时间】:2014-03-06 10:13:37
【问题描述】:

我必须将表单数据转换为 JSON 格式。我正在努力实现这一目标:-

{"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",]}}

这就是我的 JSON 的样子。我能够将数据存储在单维数组中;如何创建这样的结构?

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

【问题讨论】:

    标签: php arrays json multidimensional-array


    【解决方案1】:

    试试这个

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

    【讨论】:

    • 这不起作用 Santosh,因为该值是从文本框中获取的。那么我到底需要提供什么名字???
    • 您只需要根据 get 或 post(向服务器发送数据的类型)使用适当的变量
    【解决方案2】:

    要获取例如ip_address,您可以这样做:

    $array = json_decode($jsonstring);
    
    echo $array['connection']['ip_address']['something'];
    

    这会将您的json string 解码为一个多维数组,而您可以简单地回显它。

    对其进行编码:

    $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));
    

    要使用从表单中获取的数据,您可以这样做:

    $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);
    

    使用您需要的值编写一个表单,然后发布它们。比使用 $_POST["something"] 值创建数组并使用 json_encode(); 将此数组编码为 json

    希望这是您问题的答案。

    【讨论】:

    • 好吧,但我需要对其进行编码,而不是对其进行解码。这就是问题
    • @Abhi 更正了我的答案希望现在是解决方案
    猜你喜欢
    • 1970-01-01
    • 2016-07-10
    • 2016-01-10
    • 2012-04-15
    • 2014-06-15
    • 2017-07-03
    • 2021-08-07
    • 2020-11-11
    • 1970-01-01
    相关资源
    最近更新 更多