如何在 php 中使用带有“复杂”的 json结构?

发布于 2024-10-01 18:02:29 字数 1577 浏览 1 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心房敞 2024-10-08 18:02:29

json列表类型[]相当于php中没有键的数组。

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(...),
);

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:

$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(...),
);
如歌彻婉言 2024-10-08 18:02:29

如果您使用 PHP,我会从本机 PHP 类构造对象(json_encode 也适用于 php 对象):

class Base {
    var $id;
    var $value;
}

然后只需将这些对象放入各种数组中,您也可以使用 等方法进行抽象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):

class Base {
    var $id;
    var $value;
}

Then it's just a matter of putting these objects in various arrays, which you can also abstract with methods like addToOperation($baseObj) and addToCommands($operationObj).

You're dealing with native data structures (Arrays), so you can use native methods to remove (array_pop) and add (array_push) data.

情深如许 2024-10-08 18:02:29

像这样的东西应该可以工作

// Build up your data as a mulitdimensional array
$data = array(
    'operations' => array(
        0 => array(
           'bases' => array (
            0 => array(
                'id' => '22398',
                 'value' => 'whatever'
            ),
            1 => array(
                'id' => 'id goes here',
                 'value' => 'value goes here'
            ),
         1 => array(
            //data for operation 2
         )
);

// Then use json_encode
$json = json_encode($data);

我的语法可能并不完美,但这应该可以给你这个想法。要访问它,您可以使用类似的代码

 $operations = json_decode($data);
 foreach ($operations as $op) {
     foreach ($op->bases as $base) {
         //Logic goes here
      }
 }

希望这会有所帮助。

Something like this should work

// Build up your data as a mulitdimensional array
$data = array(
    'operations' => array(
        0 => array(
           'bases' => array (
            0 => array(
                'id' => '22398',
                 'value' => 'whatever'
            ),
            1 => array(
                'id' => 'id goes here',
                 'value' => 'value goes here'
            ),
         1 => array(
            //data for operation 2
         )
);

// Then use json_encode
$json = json_encode($data);

My syntax may not be perfect but that should give you the idea. To access it then you would use code like

 $operations = json_decode($data);
 foreach ($operations as $op) {
     foreach ($op->bases as $base) {
         //Logic goes here
      }
 }

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文