use*_*349 2 php arrays json multidimensional-array
我想对我的数据使用 json + php。我阅读了更多文档来做到这一点,基本功能是 json_decode() 和 json_encode()。我的问题是,阅读更多文档和阅读不同结构的例子给我带来了很多疑问。
我想从基础到容器创建一个这样的结构:
我脑子里的结构是这样的……
[ //The start of Commands
//Can make a property name here like "name":"puls1"
[ //Operation1
{ //Base1
"id":"22398",
"value":"255"
},
{ //Base2
"id":"22657",
"value":"80",
},
{ //Base3
"id":"7928",
"valore":"15"
}
],
[ //Operation2
{ //Base1
"id":"22398",
"value":"0"
},
{ //Base2
"id":"22657",
"value":"0",
},
{ //Base3
"id":"7928",
"valore":"0"
}
],
] //The close of Commands
Run Code Online (Sandbox Code Playgroud)
但是我已经将 [ 和 { 放在了我认为不正确的顺序中......我怎样才能制作这样的 json 结构?并在设置命令后插入新操作或删除操作?
完全谢谢..
//好的,我做了这个代码的回答
class Base
{
var $i;
var $value;
function __construct($i,$v)
{
$this->id = $i;
$this->value = $v;
}
}
$a = new Base('1','11');
$b = new Base('2','10');
$c = new Base ('3','20');
$d = new Base ('4','30');
class Operation
{
var $name;
var $values = Array();
function __construct($a)
{
$this->name = $a;
}
public function addArray($a)
{
array_push($this->values,$a);
}
}
$oper1 = new Operation("op1");
$oper1->addArray($a);
$oper1->addArray($b);
$oper2= new Operation("op2");
$oper2->addArray($c);
$oper2->addArray($d);
$commands = Array($oper1,$oper2);
echo json_encode($tot);
Run Code Online (Sandbox Code Playgroud)
现在的问题是如何进行还原操作?这样使用 json_decode 和 incapsulate 在其适当的结构中?
json 列表类型[]
相当于php 中没有key 的数组。
json 字典类型{}
等于 php 中的键控数组。
你想要的是这样的:
$json = array(
array(
array('id' => $num, 'value' => $val), // Base 1
array('id' => $num_1, 'value' => $val_1), // Base 3
array('id' => $num_2, 'value' => $val_2), // Base 2
),
array(...),
array(...),
);
Run Code Online (Sandbox Code Playgroud)