php中的有线多维数组问题
我正在尝试获取rss提要并将节点值推送到数组中。我认为我下面的php代码将创建单个数组,而不是多维数组php中的有线多维数组问题,php,rss,Php,Rss,我正在尝试获取rss提要并将节点值推送到数组中。我认为我下面的php代码将创建单个数组,而不是多维数组 //parse rss $contents= file_get_contents('http://rss..'); $xmlStr= simplexml_load_string($contents); $array=array(); foreach ($xmlStr->item as $node):
//parse rss
$contents= file_get_contents('http://rss..');
$xmlStr= simplexml_load_string($contents);
$array=array();
foreach ($xmlStr->item as $node):
$array[]=$node->title;
echo '<pre>';
print_r($array);
echo '<pre>';
endforeach;
你知道怎么改变吗?非常感谢。您需要将每个节点强制转换为一个字符串(它当前是一个SimpleXMLElement),以便在一个简单数组中返回一个普通字符串,并从
title[]
数组中检索第一项
SimpleXML在其对象上实现了\uuu toString()
魔术方法,这就是为什么它会像对print\u r()
那样响应,但要将其作为字符串使用,需要将其强制转换为字符串
foreach($xmlStr->item as$node):
//将第一个数组值强制转换为字符串
$array[]=(字符串)$node->title[0];
回声';
Array
(
[0] => SimpleXMLElement Object
(
[0] => App Store Bug Corrupts Binaries; Angry Birds Crash
)
[1] => SimpleXMLElement Object
(
[0] => In UK, HTC Defeats Apple's "Obvious" Slide Unlock Patent
)
[2] => SimpleXMLElement Object
(
[0] => WikiLeaks Begins Release of 2.5m Syrian Emails
)
[3] => SimpleXMLElement Object
(
[0] => A Critical Examination of Bill Gates' Philanthropic Record
)
[4] => SimpleXMLElement Object
(
[0] => Ask Slashdot: How Does Your Company Evaluate Your Performance?
)
[5] => SimpleXMLElement Object
(
[0] => UAV Cameras an Eye In the Sky For Adventurous Filmmakers
)
[6] => SimpleXMLElement Object
(
[0] => Copyrights To Reach Deep Space
)
[7] => SimpleXMLElement Object
(
[0] => FDA Approves HIV Home-Use Test Kit
)
[8] => SimpleXMLElement Object
(
[0] => Texas Scientists Regret Loss of Higgs Boson Quest
)
[9] => SimpleXMLElement Object
(
[0] => Icelandic MP Claims US Vendetta Against WikiLeaks
)
[10] => SimpleXMLElement Object
(
[0] => Microsoft's 'Cannibalistic Culture'
)
[11] => SimpleXMLElement Object
(
[0] => Android 4.1 Jelly Bean Review
)
)
foreach ($xmlStr->item as $node):
// Cast the first array value to a string
$array[] = (string)$node->title[0];
echo '<pre>';
print_r($array);
echo '<pre>';
endforeach;
打印(数组);
回声';
Array
(
[0] => SimpleXMLElement Object
(
[0] => App Store Bug Corrupts Binaries; Angry Birds Crash
)
[1] => SimpleXMLElement Object
(
[0] => In UK, HTC Defeats Apple's "Obvious" Slide Unlock Patent
)
[2] => SimpleXMLElement Object
(
[0] => WikiLeaks Begins Release of 2.5m Syrian Emails
)
[3] => SimpleXMLElement Object
(
[0] => A Critical Examination of Bill Gates' Philanthropic Record
)
[4] => SimpleXMLElement Object
(
[0] => Ask Slashdot: How Does Your Company Evaluate Your Performance?
)
[5] => SimpleXMLElement Object
(
[0] => UAV Cameras an Eye In the Sky For Adventurous Filmmakers
)
[6] => SimpleXMLElement Object
(
[0] => Copyrights To Reach Deep Space
)
[7] => SimpleXMLElement Object
(
[0] => FDA Approves HIV Home-Use Test Kit
)
[8] => SimpleXMLElement Object
(
[0] => Texas Scientists Regret Loss of Higgs Boson Quest
)
[9] => SimpleXMLElement Object
(
[0] => Icelandic MP Claims US Vendetta Against WikiLeaks
)
[10] => SimpleXMLElement Object
(
[0] => Microsoft's 'Cannibalistic Culture'
)
[11] => SimpleXMLElement Object
(
[0] => Android 4.1 Jelly Bean Review
)
)
foreach ($xmlStr->item as $node):
// Cast the first array value to a string
$array[] = (string)$node->title[0];
echo '<pre>';
print_r($array);
echo '<pre>';
endforeach;
endforeach;