[PHP-users 33466] Re: Undefined offsetエラーとりあえず解決

kaz okui @ sannet.ne.jp
2008年 4月 11日 (金) 16:06:57 JST


こんにちは、奥井です。
すみません、$i = 0;を忘れてました,でも$i = 0;をつけてもやはりおかしかったです。

行番号を付けてコピーします。
63 $i = 0;
64 do {
65 //
66 	$file =  $Line[$i];
67 	++$i;
68 	$title = $Line[$i];
69 	++$i;
70 	$info1 = $Line[$i];
71 	++$i;
72 	while ( !(stristr($Line[$i],"<font")) ) {
73 		$info1 .= $Line[$i];
74 		++$i;
75 	}
76 	
77 	echo $file;echo $title;echo $info1;echo "<br>";
78  } while ( $i > count($Line) );
メッセージは以下
[Fri Apr 11 14:30:52 2008] [error] [client 127.0.0.1] PHP Notice:  Undefined offset:  5 in /home/sus/public_html/get-kayouhttp.php on line 72
[Fri Apr 11 14:36:32 2008] [error] [client 127.0.0.1] PHP Notice:  Undefined offset:  5 in /home/sus/public_html/get-kayouhttp.php on line 73

確認の為に74,76行目で$iを表示させてみました。
63 $i = 0;
64 do {
65 //
66 	$file =  $Line[$i];
67 	++$i;
68 	$title = $Line[$i];
69 	++$i;
70 	$info1 = $Line[$i];
71 	++$i;
72 	while ( !(stristr($Line[$i],"<font")) ) {
73 		$info1 .= $Line[$i];echo "i=" . $i;echo "<br>";
74 		++$i;
75 	}
76 	echo "i=" . $i;echo "<br>";
77 	echo $file;echo $title;echo $info1;echo "<br>";
78  } while ( $i > count($Line) );
結果は以下です。
i=3
i=4
i=5
i=6
20080107-2130.mp3 <-$Line[0]
「バァバの石段」   <-$Line[1]
                     【作】荻原  浩<  -$Line[2]
                     【出演】山下 智子  <-$Line[3]
                         石川 寛美  <-$Line[4]
76行目でi=6になってます。i=5になると思ってたのですがUndefined offsetも5だったのでhtml文を$Lineに取り込む部分を見直してcase 0;の最後で++$j;してる為に$Line[5]が空になってました。case 0;の最後の++$j;を削除しました。それと
よくみたら72行目で次の行の<fontをチェックしてますが$Lineの最後には<fontがないので$Lineの最後を$iと比較しました。
$i = 0;
do {
	$file =  $Line[$i];
	++$i;
	$title = $Line[$i];
	++$i;
	$info1 = $Line[$i];
	++$i;
	while ( !(stristr($Line[$i],"<font")) ) {
		$info1 .= $Line[$i];
		++$i;
		if ($i == count($Line)) exit;
	}
	echo $file;echo $title;echo $info1;echo "<br>";
} while ( true );

これで思ったように表示されて Undefined offsetメッセージもでなくなりました。
ありがとうございました。



PHP-users メーリングリストの案内