0

下面是我以 JSON 格式读取 xml 和编码输出的代码

<?php
error_reporting(E_ERROR | E_PARSE);
require_once("xml2json.php");

$testXmlFile = 'http://newsonair.nic.in/NSD.asp';

$xmlStringContents = file_get_contents($testXmlFile); 
$jsonContents = "";
$jsonContents = xml2json::transformXmlStringToJson($xmlStringContents);

$tempResponseArrays = array();
$obj =json_decode($jsonContents);

foreach($obj->rss->channel->item as $item) {
 $tempResponse = array();
    if ($item->title == Dogri){
     $tempResponse['title'] = $item->title;
     $tempResponse['date'] = $item->pubDate;
     $tempResponse['link'] = $item->link;
    }
     $tempResponseArrays[] = $tempResponse;    

} 
echo json_encode(array('item' => $tempResponseArrays));

?>

上面没有 if 条件的代码给出了下面的 JSON 响应

{
"item":[
{
"title":"Arunachali : 1645-1700",
"date":"5\/7\/2013 5:11:46 PM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Arunachali-NSD-Language-Audio-Bulletins-107.mp3"
},
{
"title":"Assamese : 1905-1915",
"date":"5\/7\/2013 7:35:10 PM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Assamese-NSD-Language-Audio-Bulletins-9349.mp3"
},
{
"title":"Bengali : 0725-0735",
"date":"5\/8\/2013 7:40:38 AM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Bengali-NSD-Language-Audio-Bulletins-68.mp3"
},
{
"title":"Bengali : 1330-1340",
"date":"5\/7\/2013 2:01:28 PM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Bengali-NSD-Language-Audio-Bulletins-2844.mp3"
},
{
"title":"Bengali : 1935-1945",
"date":"5\/7\/2013 8:14:08 PM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Bengali-NSD-Language-Audio-Bulletins-70.mp3"
},
{
"title":"Dogri : 0830-0840",
"date":"5\/8\/2013 8:46:03 AM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Dogri-NSD-Language-Audio-Bulletins-71.mp3"
},
{
"title":"Dogri : 1915-1930",
"date":"5\/7\/2013 7:35:32 PM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Dogri-NSD-Language-Audio-Bulletins-373.mp3"
},
{
"title":"Kannada : 1310-1320",
"date":"5\/8\/2013 3:59:14 PM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Kannada-NSD-Language-Audio-Bulletins-3225.mp3"
},
{
"title":"Kannada : 1935 hrs",
"date":"5\/7\/2013 6:14:54 AM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Kannada-NSD-Language-Audio-Bulletins-8599.mp3"
},
{
"title":"Kashmiri : 0745-0755",
"date":"5\/8\/2013 8:24:37 AM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Kashmiri-NSD-Language-Audio-Bulletins-5374.mp3"
},
{
"title":"Kashmiri : 1825-1840",
"date":"5\/7\/2013 6:48:25 PM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Kashmiri-NSD-Language-Audio-Bulletins-1838.mp3"
},
{
"title":"Konkani : 0840-0850",
"date":"5\/8\/2013 9:40:44 AM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Konkani-NSD-Language-Audio-Bulletins-6713.mp3"
},
{
"title":"Konkani : 1830-1835",
"date":"5\/7\/2013 6:44:14 PM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Konkani-NSD-Language-Audio-Bulletins-953.mp3"
},
{
"title":"Malayalam : 0725-0735",
"date":"5\/8\/2013 9:41:33 AM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Malayalam-NSD-Language-Audio-Bulletins-8616.mp3"
},
{
"title":"Malayalam : 1250-1300",
"date":"5\/8\/2013 1:55:41 PM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Malayalam-NSD-Language-Audio-Bulletins-114.mp3"
},
{
"title":"Malayalam : 1925-1935",
"date":"5\/7\/2013 7:37:20 PM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Malayalam-NSD-Language-Audio-Bulletins-841.mp3"
},
{
"title":"Marathi : 0830-0840",
"date":"5\/7\/2013 8:46:49 AM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Marathi-NSD-Language-Audio-Bulletins-9269.mp3"
},
{
"title":"Marathi : 1330-1340",
"date":"5\/8\/2013 1:55:59 PM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Marathi-NSD-Language-Audio-Bulletins-4800.mp3"
},
{
"title":"Marathi : 2005-2015",
"date":"5\/7\/2013 8:22:01 PM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Marathi-NSD-Language-Audio-Bulletins-446.mp3"
},
{
"title":"Nepali : 1925-1935",
"date":"5\/7\/2013 7:48:38 PM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Nepali-NSD-Language-Audio-Bulletins-6898.mp3"
},
{
"title":"Odia : 0715-0725",
"date":"5\/8\/2013 8:25:05 AM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Odia-NSD-Language-Audio-Bulletins-2946.mp3"
},
{
"title":"Odia : 1350-2000",
"date":"5\/8\/2013 2:07:11 PM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Odia-NSD-Language-Audio-Bulletins-8617.mp3"
},
{
"title":"Odia : 1915-1925",
"date":"5\/7\/2013 7:40:07 PM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Odia-NSD-Language-Audio-Bulletins-2896.mp3"
},
{
"title":"Punjabi : 0830-0840",
"date":"5\/8\/2013 8:53:39 AM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Punjabi-NSD-Language-Audio-Bulletins-125.mp3"
},
{
"title":"Punjabi : 1340-1350",
"date":"5\/8\/2013 1:59:52 PM",
"link":"http:\/\/newsonair.nic.in\/writereaddata\/bulletins\/Punjabi-NSD-Language-Audio-Bulletins-124.mp3"
}
]
}

我只想从 " "title":"Dogri : 1915-1930"," 中获取标题为 "Dogri" 的项目。"Dogri" 后面的值,比如 ": 1915-1930" 总是会变化的。如何从标题字符串中获取“Dogri”?

4

1 回答 1

1

也许像这样?

if (strpos($item->title, "Dogri")===0) {
    ...
}
于 2013-05-08T12:35:52.987 回答