解析 rss - 未定义的偏移量 0

parsing rss - undefined offset 0

我有一个 rss 解析代码,但我收到有关第 38 行未定义偏移量 0 的错误消息 ($$row = $preg_match [1] [0]; )

完整的解析代码如下:

<?php


echo ' <style>
.scores {font-family:"Trebuchet MS",Verdana, Arial, sans-serif; font-size:12px;line-height:140%; }
.scores th {background:#6D8D9F; text-align:left; border:0; color:#fff;padding:2px 5px 2px 5px;}
.scores td {padding:2px 5px 2px 5px; margin:0; border:0;}
.scores td.dark {background:#D1DADF; }
.scores td.bright {background:#E0E8EF; }
.scores a {color:#ccc; text-decoration:none;}
.scores a:hover {color:#fff; text-decoration:none;}
.scores .foot {text-align:center;background:#6D8D9F; color:#ff0000}
.scores .foot small { color:#222}
.scores small {color:#777;}
</style>'."\r\n";



$rss_file = '';
$fp = fsockopen ('www.soccerstats247.com', 80, $errno, $errstr, 5);
if ($fp) {
    fputs ($fp, "GET /CompetitionFeed.aspx?langId=1&leagueId=1204 HTTP/1.0\r\nHost: www.soccerstats247.com\r\n\r\n");
    while (!feof($fp))  {
        $rss_file .= fgets($fp);
    }
    fclose($fp);
} else  {echo('RSS Error :(');}
$rss_rows = array ( "title", "link", "description", "pubDate" );
$rss_array = explode ( "<item>", $rss_file );
echo '<table class="scores"><tr><th> ';
$lineC = 0;
foreach ( $rss_array as $string ) {
    $arrData =array();
    foreach ( $rss_rows as $row ) {
        preg_match_all ( "|<$row>(.*)</$row>|", $string, $preg_match );


        $$row = $preg_match [1] [0];
        $arrData[$row] = $$row;
            }
    $tickerClass = round($lineC/2) == $lineC/2 ? 'dark' : 'bright';
    if (stripos($arrData['description'], 'SoccerStats247'  ) !== false) {
           } else {

               $result = str_replace(array('&lt;br/&gt;&lt;a rel="nofollow" href=""&gt;&lt;/a&gt;'), array(''), $arrData['description']);
    $date = date("Y.m.d", strtotime($arrData['pubDate']));

        echo '<tr><td class="'.$tickerClass.'">'.$date.' '.$result .'<br></td></tr>';
    }
    $lineC++;
}
echo '</table>';

?>

我的问题:为什么我收到错误消息?我的代码有什么问题?非常感谢。

我已经为您测试了您的代码。如果您在循环中 var_dump $preg_match,您将看到前 3 个数组包含提要或网站本身之后的信息。第 4 个循环只输出一个空数组:

array(2) {
[0] => array(0) { }
[1] => array(0) { }
}

接下来是你想知道的球队和比赛数据的实际数组。嗯是的。基本上,警告说您正在寻找的数组中没有实际条目。

最好的办法是

    if(!empty($preg_match[1]))
    {
            $$row = $preg_match [1] [0];
            $arrData[$row] = $$row;
    }

在您尝试输出或控制数据之前。