如何设置json数组在一个变量在php web服务


How to set json array in a variable in php web service?

我已经创建了一个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);

with:

$items ['items']= $data;