0

更新代码

  <?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() 的输出: img2

print_r() 的另一个输出示例: 6

4

1 回答 1

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);
于 2012-08-09T00:19:51.740 回答