Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中生成用于JSON返回的对象表_Php_Json_Rss - Fatal编程技术网

在PHP中生成用于JSON返回的对象表

在PHP中生成用于JSON返回的对象表,php,json,rss,Php,Json,Rss,我使用此脚本是为了生成json: <?php header('Content-Type: application/json'); $feed = new DOMDocument(); $feed->load("http://www.feedforall.com/sample.xml"); //$feed->load($_GET['url']); $json = array(); $json['title'] = $feed->getElementsByTagName(

我使用此脚本是为了生成json:

<?php
header('Content-Type: application/json');
$feed = new DOMDocument();
$feed->load("http://www.feedforall.com/sample.xml");
//$feed->load($_GET['url']);
$json = array();

$json['title'] =  $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
$json['description'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
$json['link'] =  $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('link')->item(0)->firstChild->nodeValue;

$items = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('item');

$json['item'] = array();
$i = 0;


foreach($items as $item) {

   $title =  $item->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
   $description = $item->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
   $pubDate = $item->getElementsByTagName('pubDate')->item(0)->firstChild->nodeValue;
   $guid = $item->getElementsByTagName('guid')->item(0)->firstChild->nodeValue;

   $json['item'][$i++]['title'] = $title;
   $json['item'][$i++]['description'] = $description;
   $json['item'][$i++]['pubdate'] = $pubDate;
   $json['item'][$i++]['guid'] = $guid;   

}


echo json_encode($json);


?>

在foreach循环中,它逐个获取提要的所有项,并将它们放在itemphp表中

当我返回时,没有表,所有项目属性都被平铺发送:

{
    "title": "FeedForAll Sample Feed",
    "description": "RSS is a fascinating technology. The uses for RSS are expanding daily. Take a closer look at how various industries are using the benefits of RSS in their businesses.",
    "link": "http://www.feedforall.com/industry-solutions.htm",
    "item": [
        {
            "title": "RSS Solutions for Restaurants"
        },
        {
            "description": "<b>FeedForAll </b>helps Restaurant's communicate with customers. Let your customers know the latest specials or events.<br>\n<br>\nRSS feed uses include:<br>\n<i><font color=\"#FF0000\">Daily Specials <br>\nEntertainment <br>\nCalendar of Events </i></font>"
        },
        {},
        {},
        {
            "title": "RSS Solutions for Schools and Colleges"
        },
        {
            "description": "FeedForAll helps Educational Institutions communicate with students about school wide activities, events, and schedules.<br>\n<br>\nRSS feed uses include:<br>\n<i><font color=\"#0000FF\">Homework Assignments <br>\nSchool Cancellations <br>\nCalendar of Events <br>\nSports Scores <br>\nClubs/Organization Meetings <br>\nLunches Menus </i></font>"
        },
        {
            "pubdate": "Tue, 19 Oct 2004 11:09:09 -0400"
        },
        {
            "guid": null
        },
        {
            "title": "RSS Solutions for Computer Service Companies"
        },
        {
            "description": "FeedForAll helps Computer Service Companies communicate with clients about cyber security and related issues. <br>\n<br>\nUses include:<br>\n<i><font color=\"#0000FF\">Cyber Security Alerts <br>\nSpecials<br>\nJob Postings </i></font>"
        },
        {
            "pubdate": "Tue, 19 Oct 2004 11:09:07 -0400"
        },
        {
            "guid": null
        },
        {
            "title": "RSS Solutions for Governments"
        },
        {
            "description": "FeedForAll helps Governments communicate with the general public about positions on various issues, and keep the community aware of changes in important legislative issues. <b><i><br>\n</b></i><br>\nRSS uses Include:<br>\n<i><font color=\"#00FF00\">Legislative Calendar<br>\nVotes<br>\nBulletins</i></font>"
        },
        {
            "pubdate": "Tue, 19 Oct 2004 11:09:05 -0400"
        },
        {
            "guid": null
        },
        {
            "title": "RSS Solutions for Politicians"
        },
        {
            "description": "FeedForAll helps Politicians communicate with the general public about positions on various issues, and keep the community notified of their schedule. <br>\n<br>\nUses Include:<br>\n<i><font color=\"#FF0000\">Blogs<br>\nSpeaking Engagements <br>\nStatements<br>\n </i></font>"
        },
        {
            "pubdate": "Tue, 19 Oct 2004 11:09:03 -0400"
        },
        {
            "guid": null
        },
        {
            "title": "RSS Solutions for Meteorologists"
        },
        {
            "description": "FeedForAll helps Meteorologists communicate with the general public about storm warnings and weather alerts, in specific regions. Using RSS meteorologists are able to quickly disseminate urgent and life threatening weather warnings. <br>\n<br>\nUses Include:<br>\n<i><font color=\"#0000FF\">Weather Alerts<br>\nPlotting Storms<br>\nSchool Cancellations </i></font>"
        },
        {
            "pubdate": "Tue, 19 Oct 2004 11:09:01 -0400"
        },
        {
            "guid": null
        },
        {
            "title": "RSS Solutions for Realtors & Real Estate Firms"
        },
        {
            "description": "FeedForAll helps Realtors and Real Estate companies communicate with clients informing them of newly available properties, and open house announcements. RSS helps to reach a targeted audience and spread the word in an inexpensive, professional manner. <font color=\"#0000FF\"><br>\n</font><br>\nFeeds can be used for:<br>\n<i><font color=\"#FF0000\">Open House Dates<br>\nNew Properties For Sale<br>\nMortgage Rates</i></font>"
        },
        {
            "pubdate": "Tue, 19 Oct 2004 11:08:59 -0400"
        },
        {
            "guid": null
        },
        {
            "title": "RSS Solutions for Banks / Mortgage Companies"
        },
        {
            "description": "FeedForAll helps <b>Banks, Credit Unions and Mortgage companies</b> communicate with the general public about rate changes in a prompt and professional manner. <br>\n<br>\nUses include:<br>\n<i><font color=\"#0000FF\">Mortgage Rates<br>\nForeign Exchange Rates <br>\nBank Rates<br>\nSpecials</i></font>"
        },
        {
            "pubdate": "Tue, 19 Oct 2004 11:08:57 -0400"
        },
        {
            "guid": null
        },
        {
            "title": "RSS Solutions for Law Enforcement"
        },
        {
            "description": "<b>FeedForAll</b> helps Law Enforcement Professionals communicate with the general public and other agencies in a prompt and efficient manner. Using RSS police are able to quickly disseminate urgent and life threatening information. <br>\n<br>\nUses include:<br>\n<i><font color=\"#0000FF\">Amber Alerts<br>\nSex Offender Community Notification <br>\nWeather Alerts <br>\nScheduling <br>\nSecurity Alerts <br>\nPolice Report <br>\nMeetings</i></font>"
        },
        {
            "pubdate": "Tue, 19 Oct 2004 11:08:56 -0400"
        },
        {
            "guid": null
        }
    ]
}
{
“标题”:“所有样本提要的提要”,
“说明”:“RSS是一项迷人的技术。RSS的用途每天都在扩大。请仔细看看各个行业是如何在其业务中利用RSS的好处的。”,
“链接”:http://www.feedforall.com/industry-solutions.htm",
“项目”:[
{
“标题”:“餐厅RSS解决方案”
},
{
“描述”:“FeedForAll帮助餐厅与顾客沟通。让顾客知道最新的特价菜或活动。
\n
\nRSS提要的用途包括:
\n日常特价菜
\n娱乐
\n活动介绍” }, {}, {}, { “标题”:“学校和学院的RSS解决方案” }, { “说明”:“FeedForAll帮助教育机构与学生沟通全校范围内的活动、事件和日程安排。
\n
\nRSS feed的用途包括:
\n家庭作业
\n学校取消
\n事件日历
\n体育成绩
\n俱乐部/组织会议
\n取消菜单” }, { “发布日期”:“2004年10月19日星期二11:09:09-0400” }, { “guid”:空 }, { “标题”:“计算机服务公司的RSS解决方案” }, { “说明”:“FeedForAll帮助计算机服务公司与客户沟通网络安全和相关问题。
\n
\n用途包括:
\n网络安全警报
\n专业
\n工作公告” }, { “发布日期”:“2004年10月19日星期二11:09:07-0400” }, { “guid”:空 }, { “标题”:“政府RSS解决方案” }, { “说明”:“FeedForAll帮助政府与公众就各种问题的立场进行沟通,并让社区了解重要立法问题的变化。
\n
\nRSS的用途包括:
\n立法日历
\nNotes
\nBulletins” }, { “发布日期”:“2004年10月19日星期二11:09:05-0400” }, { “guid”:空 }, { “标题”:“政治家的RSS解决方案” }, { “说明”:“FeedForAll帮助政客们与公众就各种问题的立场进行沟通,并随时通知社区他们的日程安排。
\n
\n用途包括:
\n日志
\n演讲
\n” }, { “发布日期”:“2004年10月19日星期二11:09:03-0400” }, { “guid”:空 }, { “标题”:“气象学家的RSS解决方案” }, { “说明”:“FeedForAll帮助气象学家与公众沟通特定地区的风暴警报和天气警报。使用RSS气象学家能够快速发布紧急和威胁生命的天气警报。
\n
\n用途包括:
\n天气警报
\n风暴警报
\n学校取消” }, { “发布日期”:“2004年10月19日星期二11:09:01-0400” }, { “guid”:空 }, { “标题”:“房地产经纪人和房地产公司的RSS解决方案” }, { “说明”:"FeedForAll帮助房地产经纪人和房地产公司与客户沟通,告知他们新推出的房产和开盘公告。RSS有助于接触目标受众,并以廉价、专业的方式传播信息。
\n
\n Feed可用于:
\n开盘日期
\n待售新房产
\n通用电气费率” }, { “发布日期”:“2004年10月19日星期二11:08:59-0400” }, { “guid”:空 }, { “标题”:“银行/抵押贷款公司RSS解决方案” }, { “说明”:“FeedForAll帮助银行、信用合作社和抵押贷款公司以迅速和专业的方式与公众沟通利率变化。
\n
\n用途包括:
\n抵押贷款利率
\n汇率
\n银行利率
\n特价” }, { “发布日期”:“2004年10月19日星期二11:08:57-0400” }, { “guid”:空 }, { “标题”:“RSS执法解决方案” }, { “说明”:FeedForAll帮助执法人员以快速有效的方式与公众和其他机构进行沟通。使用RSS,警察能够快速传播紧急和危及生命的信息。
\n
\n用途包括:
\n警报
\n X罪犯社区通知
\n警报
\n计划
\n安全警报
\n许可证报告
\n会议“ }, { “发布日期”:“2004年10月19日星期二11:08:56-0400” }, { “guid”:空 } ] }
那么错误是什么呢?我在想,将表格放在项目中应该有效,但显然不行:-/我如何使它们看起来更像这个项目中的项目

{
    "status": "ok",
    "feed": {
        "url": "http://www.feedforall.com/sample.xml",
        "title": "FeedForAll Sample Feed",
        "link": "http://www.feedforall.com/industry-solutions.htm",
        "author": "",
        "description": "RSS is a fascinating technology. The uses for RSS are expanding daily. Take a closer look at how various industries are using the benefits of RSS in their businesses.",
        "image": "http://www.feedforall.com/ffalogo48x48.gif"
    },
    "items": [
        {
            "title": "RSS Solutions for Restaurants",
            "pubDate": "2004-10-19 15:09:11",
            "link": "http://www.feedforall.com/restaurant.htm",
            "guid": "db4b074250d7eedba34de1c872724ebd",
            "author": "",
            "thumbnail": "",
            "description": "\n<b>FeedForAll </b>helps Restaurant's communicate with customers. Let your customers know the latest specials or events.<br><br>\nRSS feed uses include:<br><i>Daily Specials <br>\nEntertainment <br>\nCalendar of Events </i>\n",
            "content": "\n<b>FeedForAll </b>helps Restaurant's communicate with customers. Let your customers know the latest specials or events.<br><br>\nRSS feed uses include:<br><i>Daily Specials <br>\nEntertainment <br>\nCalendar of Events </i>\n",
            "enclosure": [],
            "categories": [
                "Computers/Software/Internet/Site Management/Content Management"
            ]
        },
        {
            "title": "RSS Solutions for Schools and Colleges",
            "pubDate": "2004-10-19 15:09:09",
            "link": "http://www.feedforall.com/schools.htm",
            "guid": "315ce482517f7e1d2d50f5a68d0e64fc",
            "author": "",
            "thumbnail": "",
            "description": "FeedForAll helps Educational Institutions communicate with students about school wide activities, events, and schedules.<br><br>\nRSS feed uses include:<br><i>Homework Assignments <br>\nSchool Cancellations <br>\nCalendar of Events <br>\nSports Scores <br>\nClubs/Organization Meetings <br>\nLunches Menus </i>\n",
            "content": "FeedForAll helps Educational Institutions communicate with students about school wide activities, events, and schedules.<br><br>\nRSS feed uses include:<br><i>Homework Assignments <br>\nSchool Cancellations <br>\nCalendar of Events <br>\nSports Scores <br>\nClubs/Organization Meetings <br>\nLunches Menus </i>\n",
            "enclosure": [],
            "categories": [
                "Computers/Software/Internet/Site Management/Content Management"
            ]
        },
        {
            "title": "RSS Solutions for Computer Service Companies",
            "pubDate": "2004-10-19 15:09:07",
            "link": "http://www.feedforall.com/computer-service.htm",
            "guid": "7973f3583dd2e760f6e1385a90cf87a8",
            "author": "",
            "thumbnail": "",
            "description": "FeedForAll helps Computer Service Companies communicate with clients about cyber security and related issues. <br><br>\nUses include:<br><i>Cyber Security Alerts <br>\nSpecials<br>\nJob Postings </i>\n",
            "content": "FeedForAll helps Computer Service Companies communicate with clients about cyber security and related issues. <br><br>\nUses include:<br><i>Cyber Security Alerts <br>\nSpecials<br>\nJob Postings </i>\n",
            "enclosure": [],
            "categories": [
                "Computers/Software/Internet/Site Management/Content Management"
            ]
        },
        {
            "title": "RSS Solutions for Governments",
            "pubDate": "2004-10-19 15:09:05",
            "link": "http://www.feedforall.com/government.htm",
            "guid": "d6131efb3d938b112ab861b97434942a",
            "author": "",
            "thumbnail": "",
            "description": "FeedForAll helps Governments communicate with the general public about positions on various issues, and keep the community aware of changes in important legislative issues. <b><i><br></i></b><br>\nRSS uses Include:<br><i>Legislative Calendar<br>\nVotes<br>\nBulletins</i>\n",
            "content": "FeedForAll helps Governments communicate with the general public about positions on various issues, and keep the community aware of changes in important legislative issues. <b><i><br></i></b><br>\nRSS uses Include:<br><i>Legislative Calendar<br>\nVotes<br>\nBulletins</i>\n",
            "enclosure": [],
            "categories": [
                "Computers/Software/Internet/Site Management/Content Management"
            ]
        },
        {
            "title": "RSS Solutions for Politicians",
            "pubDate": "2004-10-19 15:09:03",
            "link": "http://www.feedforall.com/politics.htm",
            "guid": "d01522b2d34449e665339b1c64500492",
            "author": "",
            "thumbnail": "",
            "description": "FeedForAll helps Politicians communicate with the general public about positions on various issues, and keep the community notified of their schedule. <br><br>\nUses Include:<br><i>Blogs<br>\nSpeaking Engagements <br>\nStatements<br></i>\n",
            "content": "FeedForAll helps Politicians communicate with the general public about positions on various issues, and keep the community notified of their schedule. <br><br>\nUses Include:<br><i>Blogs<br>\nSpeaking Engagements <br>\nStatements<br></i>\n",
            "enclosure": [],
            "categories": [
                "Computers/Software/Internet/Site Management/Content Management"
            ]
        },
        {
            "title": "RSS Solutions for Meteorologists",
            "pubDate": "2004-10-19 15:09:01",
            "link": "http://www.feedforall.com/weather.htm",
            "guid": "5273191196b584a4c5a98017d5a1b1d6",
            "author": "",
            "thumbnail": "",
            "description": "FeedForAll helps Meteorologists communicate with the general public about storm warnings and weather alerts, in specific regions. Using RSS meteorologists are able to quickly disseminate urgent and life threatening weather warnings. <br><br>\nUses Include:<br><i>Weather Alerts<br>\nPlotting Storms<br>\nSchool Cancellations </i>\n",
            "content": "FeedForAll helps Meteorologists communicate with the general public about storm warnings and weather alerts, in specific regions. Using RSS meteorologists are able to quickly disseminate urgent and life threatening weather warnings. <br><br>\nUses Include:<br><i>Weather Alerts<br>\nPlotting Storms<br>\nSchool Cancellations </i>\n",
            "enclosure": [],
            "categories": [
                "Computers/Software/Internet/Site Management/Content Management"
            ]
        },
        {
            "title": "RSS Solutions for Realtors &amp; Real Estate Firms",
            "pubDate": "2004-10-19 15:08:59",
            "link": "http://www.feedforall.com/real-estate.htm",
            "guid": "ce3eb8e9a2370c05babd1f4d67796e03",
            "author": "",
            "thumbnail": "",
            "description": "FeedForAll helps Realtors and Real Estate companies communicate with clients informing them of newly available properties, and open house announcements. RSS helps to reach a targeted audience and spread the word in an inexpensive, professional manner. <br><br>\nFeeds can be used for:<br><i>Open House Dates<br>\nNew Properties For Sale<br>\nMortgage Rates</i>\n",
            "content": "FeedForAll helps Realtors and Real Estate companies communicate with clients informing them of newly available properties, and open house announcements. RSS helps to reach a targeted audience and spread the word in an inexpensive, professional manner. <br><br>\nFeeds can be used for:<br><i>Open House Dates<br>\nNew Properties For Sale<br>\nMortgage Rates</i>\n",
            "enclosure": [],
            "categories": [
                "Computers/Software/Internet/Site Management/Content Management"
            ]
        },
        {
            "title": "RSS Solutions for Banks / Mortgage Companies",
            "pubDate": "2004-10-19 15:08:57",
            "link": "http://www.feedforall.com/banks.htm",
            "guid": "548e21e2baaba66f6cf56a92d72fa85a",
            "author": "",
            "thumbnail": "",
            "description": "FeedForAll helps <b>Banks, Credit Unions and Mortgage companies</b> communicate with the general public about rate changes in a prompt and professional manner. <br><br>\nUses include:<br><i>Mortgage Rates<br>\nForeign Exchange Rates <br>\nBank Rates<br>\nSpecials</i>\n",
            "content": "FeedForAll helps <b>Banks, Credit Unions and Mortgage companies</b> communicate with the general public about rate changes in a prompt and professional manner. <br><br>\nUses include:<br><i>Mortgage Rates<br>\nForeign Exchange Rates <br>\nBank Rates<br>\nSpecials</i>\n",
            "enclosure": [],
            "categories": [
                "Computers/Software/Internet/Site Management/Content Management"
            ]
        },
        {
            "title": "RSS Solutions for Law Enforcement",
            "pubDate": "2004-10-19 15:08:56",
            "link": "http://www.feedforall.com/law-enforcement.htm",
            "guid": "6e4ab6d2fee3c163f22e07ac8df11421",
            "author": "",
            "thumbnail": "",
            "description": "\n<b>FeedForAll</b> helps Law Enforcement Professionals communicate with the general public and other agencies in a prompt and efficient manner. Using RSS police are able to quickly disseminate urgent and life threatening information. <br><br>\nUses include:<br><i>Amber Alerts<br>\nSex Offender Community Notification <br>\nWeather Alerts <br>\nScheduling <br>\nSecurity Alerts <br>\nPolice Report <br>\nMeetings</i>\n",
            "content": "\n<b>FeedForAll</b> helps Law Enforcement Professionals communicate with the general public and other agencies in a prompt and efficient manner. Using RSS police are able to quickly disseminate urgent and life threatening information. <br><br>\nUses include:<br><i>Amber Alerts<br>\nSex Offender Community Notification <br>\nWeather Alerts <br>\nScheduling <br>\nSecurity Alerts <br>\nPolice Report <br>\nMeetings</i>\n",
            "enclosure": [],
            "categories": [
                "Computers/Software/Internet/Site Management/Content Management"
            ]
        }
    ]
}
{
“状态”:“确定”,
“提要”:{
“url”:”http://www.feedforall.com/sample.xml",
“标题”:“所有样本提要的提要”,
“链接”:http://www.feedforall.com/industry-solutions.htm",
“作者”:“作者”,
“说明”:“RSS是一项迷人的技术。RSS的用途每天都在扩大。请仔细看看各个行业是如何在其业务中利用RSS的好处的。”,
“图像”:http://www
$json['item'][$i++]['title'] = $title;
$json['item'][$i++]['description'] = $description;
$json['item'][$i++]['pubdate'] = $pubDate;
$json['item'][$i++]['guid'] = $guid; 
$json['item'][$i]['title'] = $title;
$json['item'][$i]['description'] = $description;
$json['item'][$i]['pubdate'] = $pubDate;
$json['item'][$i]['guid'] = $guid; 
# Increment the counter for the next iteration
$i++;
$json['item'][$i++] = array(
  'title' => $title,
  'description' => $description,
  'pubdate' => $pubDate,
  'guid' => $guid,
);
$json['item'][$i]['title'] = $title;
$json['item'][$i]['description'] = $description;
$json['item'][$i]['pubdate'] = $pubDate;
$json['item'][$i]['guid'] = $guid;  
$i++;