1

I need to create json with multiple levels via PHP.

{
    "accident": {
        "dateTime": "2015-08-08T16:12:08",
        "desc": "string"
    },
    "calculationInput": {
        "idBlockCodes": [
            {
                "code": 51,
                "value": 1
            }
        ],
        "wageRates": {
            "labourRate1": 10,
            "labourRate2": 11,
            "labourRate3": 22,
            "labourRate4": 33,
            "labourRateD": 44,
            "paintRate": 55
        }
    },
    "caseNumber": "F4Q",
    "vehicle": {
        "adminData": {
            "firstRegDate": "2015-07-08",
            "numberPlate": "MI214AF"
        },
        "identification": {
            "vin": "VF7S6NFZF56215577"
        },
        "owner": {
            "city": "Praha",
            "country": "CZE",
            "email": "[email protected]",
            "name": "Fero Taraba",
            "phone": "777111222",
            "phone2": "999555333",
            "postCode": "07101",
            "street": "Kukureliho 18"
        }
    }
}

I actuyltry this and it's possible for me to create single level json, with other words:

$data = array (
        "caseNumber" =>"F4Q"    
        );

and then just easy:

$data_json= json_encode($data);

But can someone explain me how can I do this second or even 3th level tree to convert to JSON?

I will really appriciate your help guys. Thanks

EDIT: Most of the answers really lead me right way, only problem now is this part:

  "idBlockCodes": [
                {
                    "code": 51,
                    "value": 1
                }
            ],

where there is [ ] which represent some list of blockCodes. Any idea what to do with this ? :)

1
  • is that [] what you want to get? Commented Aug 14, 2015 at 9:15

5 Answers 5

1

What is your question exactly?

You can create a array and then just encoding it to json?

$data = array(
    'Key 1' => array(
         'Key 2 - Key 1' => array(
              'Key 3 - Key 1' => array()
          )
    )
);
echo json_encode($data);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Youri, but I afriad this will not fight correctly with this part: "idBlockCodes": [ { "code": 51, "value": 1 } ],
1

Just use associative arrays

$data = array (
        "caseNumber" =>"F4Q",    
        "vehicle" => array (
            "adminData"=>
                     array(
                      "firstRegDate"=> "2015-07-08",
                       "numberPlate"=> "MI214AF"
                     ),
              "identification" =>
                      array(
                      "vin"=> "VF7S6NFZF56215577"
                      )      
            )
        );

print out

{"caseNumber":"F4Q","vehicle":{"adminData":{"firstRegDate":"2015-07-08","numberPlate":"MI214AF"},"identification":{"vin":"VF7S6NFZF56215577"}}}

1 Comment

Hi, thank you for your answer it definitley make sense, but may I ask what to do with [ ] this kind of brackets? :) Please check my edit in question
1

Use associative Arrays:

$data = array (
    "caseNumber" =>"F4Q",    
    "vehicle" => array (
        "adminData"=>
                 array(
                  "firstRegDate"=> "2015-07-08",
                   "numberPlate"=> "MI214AF"
                 ),
          "identification" =>
                  array(
                  "vin"=> "VF7S6NFZF56215577"
                  )      
        )
    );

echo json_encode($data)

3 Comments

Hi, thank you for your answer it definitley make sense, but may I ask what to do with [ ] this kind of brackets? :) Please check my edit in question
I think that is a notAssociative array in php? Always avoided such lists in JSON Arrays..
thanks for advise I give you thumb up for answer, anyway It's not my choose how JSON have to looks like, because this is what REST service expect I just need to make it in PHP :)
1

you get your brackets by putting an additional array around your object.

$data = array("idBlockCodes" => array(
            array(
                "code" => 51,
                "value" => 1
            )
        )
  );

 print json_encode($data);

1 Comment

Dispite the fact it's other brackets this time? [ instead of { ? :)
0

For you to convert all of your data to array:

You must first use json_encode();

   $a = json_encode($yourData);

And then decode it:

   $b = json_decode($a, true);
   echo '<pre>';
   print_r($b); //print it

It displays associated arrays with it

Comments

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.