当前位置: 代码迷 >> PHP >> 格式化这样的一个json串,语法该如何写?(急)
  详细解决方案

格式化这样的一个json串,语法该如何写?(急)

热度:24   发布时间:2016-04-28 19:33:27.0
求助:格式化这样的一个json串,语法该怎么写?(急!)
例如有一个二维数组 
$myarrnews=array();
$myarrnews =explode("||**||", $mystr); //内容来自$mystr,包括多条新闻记录
$myarrline=array();  
$myarrline =explode("||%%||", $myarrnews[$i]);      //$myarrline 是其中的一条记录,包括多个字段
print_r($myarrline);                     //每个新闻各个字段的集合,例如:Array ( [0] => 10 [1] => 2166 [2] => 创新。。。
现在需要这样的一个json串,格式为:(暂时设定为每页显示5条)
{
    "ret": 0, 
    "data": {
        "newslist": [
            {
                "title": "第1条新闻标题", 
                "commentcount": 0, 
                "source": "来源", 
                "nid": 10, 
                "digest": "新闻简介", 
                "ptime": "2012-03-28 14:11:43"
            }, 
            {
                "title": "第2条新闻标题", 
                "commentcount": 0, 
                "source": "来源", 
                "nid": 9, 
                "digest": "新闻简介", 
                "ptime": "2012-03-28 13:12:56"
            }, 
            {
                "title": "第3条新闻标题", 
                "commentcount": 0, 
                "source": "来源", 
                "nid": 8, 
                "digest": "新闻简介", 
                "ptime": "2012-03-28 12:21:11"
            }, 
            {
                "title": "第4条新闻标题", 
                "commentcount": 0, 
                "source": "来源", 
                "nid": 7, 
                "digest": "新闻简介", 
                "ptime": "2012-03-28 11:21:00"
            }, 
            {
                "title": "第5条新闻标题", 
                "commentcount": 1, 
                "source": "来源", 
                "nid": 5, 
                "digest": "新闻简介", 
                "ptime": "2012-02-18 19:20:01"
            }
        ], 
        "totalnum": 5
    }, 
    "msg": "ok"
}
//----------------------------
php语法该如何书写呢?请大家帮忙,急用!
------解决方案--------------------
$ar = arrat(
  ' ret' => 0, 
  'data' => array(
     'newslist' => $newslist,