更新代码
<?php
#header( "Content-Type: application/xml" );
function doLoop($arr = array())
{
global $newsStory;
foreach( $arr as $r )
{
//check if we're dealing with an array (multiple links)
if ( is_array($r) === true )
{
//loop through the array and work with each link
foreach ( $r as $link_)
//check if link is an array
if ( is_array($link_) ){doLoop($link_); continue;}
// //end of third dimension array found
## gets url from database as outlined above.
$xmlUrl = $link_;
#Loads the url above into XML
$ConvertToXml = simplexml_load_file($xmlUrl);
# -> Setup XML
$newsStory[] = $ConvertToXml->channel->item;
}//end of loop
continue;//move on
}//end of is array
//if we get here, we know that only $r is not an array, just a value, so:
## gets url from database as outlined above.
$xmlUrl = $r;
#Loads the url above into XML
#$ConvertToXml = simplexml_load_file($xmlUrl);
# -> Setup XML
#$newsStory[] = $ConvertToXml->channel->item;
$newsStory[] = $r;
print_r($newsStory);
}//end of function
## Loop through results from mysql
try{
#connection string
// $dbconn = new PDO('mysql:host=localhost;port=3306;dbname=thedb',array(PDO::ATTR_PERSISTENT => true));
$dbconn = new PDO('mysql:host=localhost;port=3306;dbname=thedb','root','toshiba1',array(PDO::ATTR_PERSISTENT => true));
$q = $dbconn->prepare("SELECT FW_ArtSrcLink FROM FW_ArtSrc WHERE OneSet=1 and leagID=20");
#call stored proc
$q->execute();
#get the rows into an array
$result = $q->fetchAll();
$newsStory = array();
doLoop($result);
# -----> Load News Stories
for($i = 0;$i<sizeof($newsStory); $i++){
//print_r($newsStory);
echo "<a href='".$newsStory[$i]->link."'>".$newsStory[$i]->title."</a><br />";
echo $newsStory[$i]->description;
echo '<hr>';
} // for()
} // try
catch(Exception $e){
$errorPg='errors/fanwire_loop.php';
$pageDateOfError = $e->getMessage().'on:'.'aggregate_looping.php'.' on '.date('l jS \of F Y h:i:s A'); # inc. details of error
file_put_contents($errorPg,$pageDateOfError, FILE_APPEND | LOCK_EX);
} // catch
?>
print_r()的输出:
print_r()输出的另一个例子:
答案 0 :(得分:1)
这开始变得非常混乱。
你的代码很乱,所以我将在这里开始。
你说print_r($result)
会返回:
Array ( [0] => Array ( [FW_ArtSrcLink] => http://sports.espn.go.com/espn/rss/tennis/news [0] => http://sports.espn.go.com/espn/rss/tennis/news ) [1] => Array ( [FW_ArtSrcLink] => http://sports.yahoo.com/tennis/rss.xml [0] => http://sports.yahoo.com/tennis/rss.xml ) [2] => Array ( [FW_ArtSrcLink] => http://bleacherreport.com/articles;feed?tag_id=12 [0] => http://bleacherreport.com/articles;feed?tag_id=12 )
因此,var中有三个数组,每个数组包含两个链接。
因此,两个简单的foreach
循环应该能够轻松解决这个问题:
foreach ($result as $value )
{
if ( is_array($value) )
{
foreach ( $value as $secondValue )
{
}
continue;
}
}
应该那么简单。
我们将在函数中进行所有处理。
function processLink( $link , $appendArr )
{
## gets url from database as outlined above.
$xmlUrl = $link;
#Loads the url above into XML
$ConvertToXml = simplexml_load_file($xmlUrl);
# -> Setup XML
$appendArr[] = $ConvertToXml->channel->item;
}
所以,最终结果应该是:
function processLink( $link , $appendArr )
{
## gets url from database as outlined above.
$xmlUrl = $link;
#Loads the url above into XML
$ConvertToXml = simplexml_load_file($xmlUrl);
# -> Setup XML
$appendArr[] = $ConvertToXml->channel->item;
}
$dbconn = new PDO('mysql:host=localhost;port=3306;dbname=thedb','root','toshiba1',array(PDO::ATTR_PERSISTENT => true));
$q = $dbconn->prepare("SELECT FW_ArtSrcLink FROM FW_ArtSrc WHERE OneSet=1 and leagID=20");
$q->execute();
$result = $q->fetchAll();
$newsStory = array();
foreach ($result as $value )
{
if ( is_array($value) )
{
foreach ( $value as $secondValue )
{
processLink($secondValue , &$newsStory);
}
continue;
}
processLink($value , &$newsStory);
}
print_r($newsStory);