json字符串拼接 json_encode 數組帶有花括號{}和中括號[]

字母索引的數組的是花括號,數字索引纔是中括號php

數字索引的數組示例:json

$p = array('name'=>'yu','age'=>'23');
$data[0]['name']='zhangsan';
$data[0]['orderDate']='2018-02';
$data[0]['price']='58';
$data[0]['prodcut'][0]['prodcut_id']='101';
$data[0]['prodcut'][0]['prodcut_price']='28';
$data[0]['prodcut'][0]['prodcut_name']='毛巾';
$data[0]['prodcut'][1]['prodcut_id']='102';
$data[0]['prodcut'][1]['prodcut_price']='30';
$data[0]['prodcut'][1]['prodcut_name']='浴巾';

$data=json_encode($data);
echo $data;
exit;
//結果以下:
[{"name":"zhangsan","orderDate":"2018-02","price":"58","prodcut":[{"prodcut_id":"101","prodcut_price":"28","prodcut_name":"\u6bdb\u5dfe"},{"prodcut_id":"102","prodcut_price":"30","prodcut_name":"\u6d74\u5dfe"}]}]

字母索引的數組示例:數組

$p = array('name'=>'yu','age'=>'23');
$data=json_encode($p);
echo $data;
exit;
//結果以下:
{"name":"yu","age":"23"}
相關文章
相關標籤/搜索