【发布时间】:2011-05-08 23:02:39
【问题描述】:
我想为我的数据使用 json + php。我阅读了更多文档来执行此操作,基本函数是 json_decode() 和 json_encode()。我的问题是,阅读更多文档和阅读不同的结构示例让我产生了很多疑问。
我想创建一个这样的结构,从基础到容器:
- 有一个 Base,有 2 个属性:id 和 value
- 有一个可以有多个 Base 的操作
- 有一个命令可以有多个操作(如果可能,还有一个属性调用名称)
我脑海中的结构是这样的……
[ //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
但我认为 [ 和 { 的顺序不正确... 我怎样才能制作这样的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);
现在的问题是如何进行还原操作?这样使用json_decode并封装在其适当的结构中?
【问题讨论】:
标签: php arrays json multidimensional-array