如何在 php 中使用带有“复杂”的 json结构?
我想使用 json + php 来存储我的数据。我阅读了更多文档来做到这一点,基本函数是 json_decode() 和 json_encode()。我的问题是,阅读更多的文档并阅读不同的结构示例给我带来了很多疑问。
我想创建一个像这样的结构,从基础到容器:
- 有一个基础,有2个属性:id和值
- 有一个可以有多个基础的操作
- 有一个可以有多个操作的命令(如果可能的话)属性 callad 名称)
我脑海中的结构是这样的...
[ //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 并将其封装在适当的结构中?
i want use json + php for my data. I read more document to do this, and the basic function are json_decode() and json_encode(). My problem is that, read more document and read different example of structure have created in me a lot of doubts.
I want create a structure like this begine from the basic to the container:
- there is a Base, that have 2 property: id and value
- there is a Operations that can have multiple Base
- there is a Command that can have multiple Operations (and if possible a property callad name)
the structure in my mind is like this...
[ //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
But i have put the [ and { in the not correct order i think...
How can i make a json structure like this? And after set a command to insert a new Operation or remove Operation?
Thank's at all..
//Ok by answer of i made this code
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);
Now the problem is how can i make the revert operation? Such a use of json_decode and incapsulate in its appropriate structure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
json列表类型
[]
相当于php中没有键的数组。json 字典类型
{}
相当于 php 中的键控数组。你想要的是这样的:
The json list type
[]
is equal to a array without keys in php.The json dictionary type
{}
is equal to a keyed array in php.What you want is something like this:
如果您使用 PHP,我会从本机 PHP 类构造对象(json_encode 也适用于 php 对象):
然后只需将这些对象放入各种数组中,您也可以使用
等方法进行抽象addToOperation($baseObj) 和 addToCommands($operationObj)。
您正在处理本机数据结构(数组),因此您可以使用本机方法删除(array_pop)和添加(array_push)数据。
If you're working with PHP I would construct the objects from native PHP Classes (json_encode works with php objects as well):
Then it's just a matter of putting these objects in various arrays, which you can also abstract with methods like
addToOperation($baseObj)
andaddToCommands($operationObj)
.You're dealing with native data structures (Arrays), so you can use native methods to remove (array_pop) and add (array_push) data.
像这样的东西应该可以工作
我的语法可能并不完美,但这应该可以给你这个想法。要访问它,您可以使用类似的代码
希望这会有所帮助。
Something like this should work
My syntax may not be perfect but that should give you the idea. To access it then you would use code like
Hope this helps.