如果孩子的attritbutes是空的,我试图删除一个节点。
XML是一个带有simpleXML的http请求,我无法在此处粘贴。 因此我从网址粘贴了一些XML文档:
XML
<game id="1421692" status="Pre-Game" summary="1:00 PM" PreRegPost="Regular Season" PreRegPostID="2" GameDate="11/2/2014 1:00:00 PM">
<HomeTeam HomeTeam="Vikings" HomeTeamAbr="Min" HomeTeamID="16" HomeScore="0" Home_Outcome="undecided">HomeTeam</HomeTeam>
<AwayTeam AwayTeam="Redskins" AwayTeamAbr="Was" AwayTeamID="28" AwayScore="0" Away_Outcome="undecided">AwayTeam</AwayTeam>
game
</game>
<game id="" status="" summary="" PreRegPost="" PreRegPostID="2" GameDate="">
<HomeTeam HomeTeam="" HomeTeamAbr="" HomeTeamID="16" HomeScore="" Home_Outcome="">HomeTeam</HomeTeam>
<AwayTeam AwayTeam="" AwayTeamAbr="" AwayTeamID="" AwayScore="" Away_Outcome="">AwayTeam</AwayTeam>
game
</game>
<game id="1421668" status="Pre-Game" summary="1:00 PM" PreRegPost="Regular Season" PreRegPostID="2" GameDate="11/16/2014 1:00:00 PM">
<HomeTeam HomeTeam="Bears" HomeTeamAbr="Chi" HomeTeamID="3" HomeScore="0" Home_Outcome="undecided">HomeTeam</HomeTeam>
<AwayTeam AwayTeam="Vikings" AwayTeamAbr="Min" AwayTeamID="16" AwayScore="0" Away_Outcome="undecided">AwayTeam</AwayTeam>
game
</game>
PHP
foreach ($game->{'info-schedule'}[0]->game as $games) {
//Get the Date and Time for the game
$time = $games->attributes()->GameDate;
//Get the HomeTeam Data
$hometeam = $games->HomeTeam->attributes()->HomeTeamAbr;
$hometeamscore = $games->HomeTeam->attributes()->HomeTeamScore;
//Get the AwayTeam Data
$awayteam = $games->AwayTeam->attributes()->AwayTeamAbr;
$awayteamscore = $games->AwayTeam->attributes()->AwayTeamScore;
//Change TimeZone
$playtime = new DateTime($time, new DateTimeZone('America/Los_Angeles'));
$playtime->setTimezone(new DateTimeZone('Europe/Copenhagen'));
$playtimes .= "<li class='time'>".$playtime->format('d/m - H:i')."</li>";
$versus .= "<li class='versus'>".$hometeam."<span class='HomeTeamScore'>".$hometeamscore."</span><span class ='AwayTeamScore'>".$awayteamscore."</span>".$awayteam."</li>";
}
HTML
<div class="results">
<ul>
<?php echo $playtimes; ?>
</ul>
<ul>
<?php echo $versus; ?>
</ul>
</div>
我的输出电流如下:
02/11 - 22:00 / MinWas
16/06 - 00:37 / //This is supposed to come out all empty
16/11 - 22:00 / ChiMin
问题在于,在空节点中,它会自动输出我的本地时间,即使它是空的。
如何转换或删除该空节点?
PS。这是我的第二个foreach循环 - 所以不要判断它:)
答案 0 :(得分:0)
如果远程队伍不存在,请使用continue
关键字跳过迭代。
foreach ($game->{'info-schedule'}[0]->game as $games) {
//Get the Date and Time for the game
$time = $games->attributes()->GameDate;
//Get the HomeTeam Data
$hometeam = $games->HomeTeam->attributes()->HomeTeamAbr;
$hometeamscore = $games->HomeTeam->attributes()->HomeTeamScore;
if (empty($games->AwayTeam->attributes()->AwayTeamAbr)) {
continue;
}
//Get the AwayTeam Data
$awayteam = $games->AwayTeam->attributes()->AwayTeamAbr;
$awayteamscore = $games->AwayTeam->attributes()->AwayTeamScore;
//Change TimeZone
$playtime = new DateTime($time, new DateTimeZone('America/Los_Angeles'));
$playtime->setTimezone(new DateTimeZone('Europe/Copenhagen'));
$playtimes .= "<li class='time'>".$playtime->format('d/m - H:i')."</li>";
$versus .= "<li class='versus'>".$hometeam."<span class='HomeTeamScore'>".$hometeamscore."</span><span class ='AwayTeamScore'>".$awayteamscore."</span>".$awayteam."</li>";
}
答案 1 :(得分:0)
您只需要进行简单的验证检查即可确定是否处理foreach迭代。根据我的理解,您可以在添加结果之前检查$hometeam
是否为空(未设置,空白,空,零,假等)。如果使用continue()
:
foreach ($game->{'info-schedule'}[0]->game as $games) {
if(empty($hometeam))
continue;
// rest of your code
}
你应该记住的是,像这样的大型连接很好但是有点乱,你应该考虑使用这样的数组:
$play_time = array();
$versus = array();
foreach ($game->{'info-schedule'}[0]->game as $games) {
if(empty($hometeam))
continue;
// rest of your code
$play_time[] = "<li class='time'>".$playtime->format('d/m - H:i')."</li>";
$versus[] = "<li class='versus'>".$hometeam."<span class='HomeTeamScore'>".$hometeamscore."</span><span class='AwayTeamScore'>".$awayteamscore."</span>".$awayteam."</li>";
}
然后您输出如下(使用implode()
)(注意:您可以使用\n
或PHP_EOL
分隔符让您的输出在源代码中更清晰一些):
<div class="results">
<ul>
<?php echo implode($play_time); ?>
</ul>
<ul>
<?php echo implode($versus) ?>
</ul>
</div>
另一方面也注意到,使用sprintf()
调用将格式化内容插入字符串中可能更容易表达HTML块:
$pt_format = '<li class="time">%s</li>';
$vs_format = '<li class="versus">%s<span class="HomeTeamScore">%s</span><span class="AwayTeamScore">$s</span>$s</li>';
foreach ($game->{'info-schedule'}[0]->game as $games) {
if(empty($hometeam))
continue;
// rest of your code
$play_time[] = sprintf($pt_format, $playtime->format('d/m - H:i'));
$versus[] = sprintf($vs_format, $hometeam, $hometeamscore, $awayteamscore, $awayteam);
}
只是一个想法:)这样更容易阅读。