Having trouble with PHP and JSON

时间:2015-07-31 20:41:42

标签: php arrays json

I have to create a JSON request in PHP based on this encoded JSON layout and I'm having difficulty preparing the request in the pre-encoded PHP. Any help would be greatly appreciated as what I am trying to put together does not see to be working:

{   
    "skin":"weborder",  
    "establishmentId":1,    
    "items":[{  
        "modifier_amount":0,    
        "modifieritems":[   

        ],  
        "initial_price":0.1,    
        "special_request":"",   
        "price":0.1,    
        "product":null, 
        "product_name_override":"Bag Charge",   
        "quantity":1,   
        "tax_amount":0, 
        "tax_rate":0,   
        "is_cold":false 
    },
    {   
        "modifier_amount":0.5,  
        "modifieritems":[{  
            "modifier":106, 
            "modifier_cost":0,  
            "modifier_price":0.5,   
            "qty":1,    
            "qty_type":0    
        }], 
        "initial_price":1.95,   
        "special_request":"",   
        "price":1.95,   
        "product":1,    
        "product_name_override":"Regular Coffee",   
        "quantity":1,   
        "tax_amount":0.197, 
        "tax_rate":8.75,    
        "is_cold":false 
    }], 
    "orderInfo":{   
        "created_date":"2014-­‐06-­‐11T18:52:44",   
        "pickup_time":"2014-­‐06-­‐11T19:22:44",    
        "tax":0.2,  
        "subtotal":2.55,    
        "final_total":2.55, 
        "surcharge":0,  
        "dining_option":0,  
        "call_name":"Joe Smith / Jun 11, 7:22pm / 1234567890"   
    },  
    "paymentInfo":{ 
        "type":3,   
        "phone":"1234567890",   
        "email":"joe@mail.com", 
        "first_name":"Joe", 
        "last_name":"Smith" 
    },  
    "notifications":[{  
        "skin":"weborder",  
        "type":"email",
        "destination":"joe@mail.com"    
    }]  
}

Here is the PHP I put together that is breaking:

$order_data = array(
            'skin' => 'weborder',   
            'establishmentId' => $establishment,
            'items' => array(
                'modifier_amount':0,    
                'modifieritems' => array(   
                    'modifier' => '106',    
                    'modifier_cost' => '0', 
                    'modifier_price' => '0.5',  
                    'qty' => '1',   
                    'qty_type' => '0'   
                ),  
                'initial_price' => '0.1',   
                'special_request' => '',    
                'price' => '0.1',   
                'product' => null,  
                'product_name_override' => 'Bag Charge',    
                'quantity' => '1',
                'tax_amount' => '0',    
                'tax_rate' => '0',  
                'is_cold"' => 'false'   
             ),
             'orderInfo' => array(
                'created_date' => '2014-­‐06-­‐11T18:52:44',    
                'pickup_time' => '2014-­‐06-­‐11T19:22:44', 
                'tax' => '0.2', 
                'subtotal' => '2.55',   
                'final_total' => '2.55',    
                'surcharge' => '0', 
                'dining_option' => '0', 
                'call_name' => 'Joe Smith / Jun 11, 7:22pm  / 1234567890'
             ),
             'paymentInfo' => array(
                'type' => 3,    
                'phone' => '1234567890',    
                'email' => 'joe@mail.com',  
                'first_name' => 'Joe',  
                'last_name' => 'Smith'  
             ),
             'notifications' => array(
                'skin:' => 'weborder',  
                'type' => 'email',
                'destination' => 'joe@mail.com'
             )
        );

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

Thanks in advance. :)  

1 个答案:

答案 0 :(得分:2)

我想你会从中得到这个想法!修改值并添加练习中缺少的内容。

$array = 
[
    'skin' => 'weborder',
    'establishmentId' => 1,
    'items' =>
    [
        [
            'modifier_amount' => 0,
            'modifieritems' => [],
            'initial_price' => 0.1
        ],
        [
            'modifier_amount' => 0.5,
            'modifieritems' =>
            [
                [
                    'modifier' => 106,
                    'modifier_cost' => 0
                ]
            ],
            'initial_price' => 1.95
        ]
    ],
    'orderInfo' =>
    [
        'created_date' => '2014-­‐06-­‐11T18:52:44',
        'pickup_time' => '2014-­‐06-­‐11T19:22:44'
    ],
    'paymentInfo' =>
    [
        'type' => 1,
        'phone' => '123455'
    ],
    'notifications' =>
    [
        [
            'skin' => 'web',
            'type' => 'werewwer'
        ]
    ]
];


echo json_encode($array);