PHP:将元数据数组添加到JSON数组

时间:2017-12-15 09:38:34

标签: php arrays json

我用PHP获得了以下JSON数组:

 "data": [
    {
        "RecordID": 1,
        "OrderID": "53150-422",
        ...
    },
    {
        ...
    },

我这样做:

echo json_encode(array('data' => $users),JSON_PRETTY_PRINT);

现在我想像这样向数组添加元数据:

{
"meta": {
    "page": 1,
    "pages": 1,
    "perpage": -1,
    "total": 350,
    "sort": "asc",
    "field": "RecordID"
},
"data": [
    {
        "RecordID": 1,
        "OrderID": "53150-422",
        ...
    },

如何做到这一点?提前谢谢!

1 个答案:

答案 0 :(得分:1)

以这种方式编写代码

<?php
$meta=[ "page"=> 1,
    "pages"=> 1,
    "perpage"=> -1,
    "total"=> 350,
    "sort"=> "asc",
    "field"=> "RecordID"];
$users= [

        "RecordID"=> 1,
        "OrderID"=> "53150-422"

];

echo json_encode(array('meta'=>$meta,'data' => $users),JSON_PRETTY_PRINT);    
?>