0

私はこの通知を持っています:

注意: 未定義のオフセット: 1 行目の D:\servers\data\localweb\alexa traffic.php の 14

14行目はこれです:

$usrank = ($rankus[1]) ? $rankus[1] : 0;

これを修正するにはどうすればよいですか?

これが私のコードです:

<?php
$source = file_get_contents('http://data.alexa.com/data?cli=10&dat=snbamz&url=linuxplained.com');

//Alexa Rank
preg_match('/\<popularity url\="(.*?)" text\="([0-9]+)" source\="panel"\/\>/si', $source, $matches);
$aresult = ($matches[2]) ? $matches[2] : 0;

//Alexa Sites Linking in
preg_match('/\<linksin num\="([0-9]+)"\/\>/si', $source, $asli);
$alinksin = ($asli[1]) ? $asli[1] : 0;

//Alexa US Rank
preg_match('/\<country code\="US" name\="United States" rank\="([0-9]+)"\/\>/si', $source, $rankus);
$usrank = ($rankus[1]) ? $rankus[1] : 0;

//Alexa Reach Rank
preg_match('/\<reach rank\="([0-9]+)"\/\>/si', $source, $reachr);
$areach = ($reachr[1]) ? $reachr[1] : 0;
?>
4

2 に答える 2

3

isset()その値が存在するかどうかを確認するために使用します。

$usrank = (isset($rankus[1])) ? $rankus[1] : 0;
于 2014-05-20T18:03:19.903 に答える
0

This error occurs because the array $rankus doesn't have a value at index 1. The easiest fix is by using isset to check whether the index exists before attempting to use it. So then your could would be:

$usrank = (isset($rankus[1])) ? $rankus[1] : 0;

This uses the ternary operator, which is equivalent to the following (easier to understand) code:

$usrank;
if (isset($rankus[1]) {
    $usrank = $rankus[1];
} else {
    $usrank = 0;
}

Hopefully you now understand why the problem occurs and how to fix this.

There are more problems in your code though. When you create the variables $aresult, $alinksin and $areach, you don't check whether the needed indexes exist either. You should probably do this in order to avoid more errors like the one you get at the moment.

Finally, I noticed you are trying to parse an XML document using regular expressions. That can go wrong in a lot of ways, and it's better to use a 'real' XML parser. Take a look at SimpleXML or one of the other XML libraries for PHP!

于 2014-05-20T18:20:36.357 に答える