如何在php web服务中设置变量中的json数组?

如何在php web服务中设置变量中的json数组?,php,arrays,json,Php,Arrays,Json,我创建了一个php函数来从wordpress获取数据。我已经使用了下面的函数来获取数据,但这些数据没有任何变量: function RecentPost() { $my_posts = get_posts(array('numberposts' => 5, 'orderby' => 'post_date', 'order' => 'DESC', 'post_type' => 'post', 'post_status

我创建了一个php函数来从wordpress获取数据。我已经使用了下面的函数来获取数据,但这些数据没有任何变量:

function RecentPost()
 {

$my_posts = get_posts(array('numberposts' => 5,
   'orderby'     => 'post_date',
    'order'       => 'DESC',
    'post_type'   => 'post',
    'post_status' => 'publish'));

foreach($my_posts as $post) {
      $data[] = 
        array(
           "id" => $post->ID,
          "title" => $post->post_title,
         "image" => wp_get_attachment_image_src( get_post_thumbnail_id($post->ID,'thumbnail')),

        );
       }

echo json_encode($data);
 }
输出:

[
    {
        "id": 7101,
        "title": "BLACK FOREST CAKE ",
        "image": [
            "http://www.....jpg",
            150,
            150,
            true
        ]
    },
    {
        "id": 7100,
        "title": "MANGOCHIAN- IN GRAVY",
        "image": [
            "http://www.....jpg",
            150,
            150,
            true
        ]
    }
]

我想将我的json放入一些变量中,比如json:

{
    "next": {
        "$ref": "https://....."
    },
    "items": [
        {
            "empno": 7839,
            "ename": "KING",
            "job": "PRESIDENT",
            "hiredate": "1981-11-17T00:00:00Z",
            "sal": 5000,
            "deptno": 10
        }

    ]
}

有人能告诉我如何做到这一点吗?

我自己找到了答案:

我替换了这一行:echo json_encode($data)

与:

$items['items']=$data

echo json_编码($items)