2

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?

3 Answers 3

5

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(...),
);
Sign up to request clarification or add additional context in comments.

1 Comment

Very nice this code...if i want get a value a can use $json[0][0]->id; Only a question is there a way to add a property to the array(...) in my case to the operations?
2

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.

1 Comment

With your code i try this class Base { var $id; var $value; function __construct($i,$v) { $this->id = $i; $this->value = $v; } } $a = new Base('1','11'); $b = new Base('2','10'); $commans1 = Array($a,$b); $commans2 = Array($a,$b); $tot = Array($commans1,$commans2); echo json_encode($tot); How can i add a property name to the operations?
1

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.

3 Comments

Yes, but i want a complete example of manager that structure...for example how can i add another Commands?
just put commands above everything else $data = array('commands' => array(0 => array). I think that that is the best managment that you can come up with
If you want to give the command a name you can add name before the operations as a property of the commands. Think of your structure as a multidimensional array, build that array and use json_encode is the best technique for complex data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.