[英]Defining multidimensional array while storing form data in JSON format in php
我必須將表單數據轉換為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"]]
要獲取例如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 json_encode();
希望這是您問題的答案。
試試這個
$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.