我在我的foreach中有以下内容....这有效,但每个$ value有两组值,它只显示第一个
<?php
$info = simplexml_load_file("https://api.website.co.za/ACCESS_GetAccountSessions?");
echo "<ul info>";
foreach ($info->sessions as $sessions):
$count = $sessions->{'session-count'};
$ip = $sessions->session->{'ip-address'};
$nas = $sessions->session->{'nas-ip-address'};
$port = $sessions->session->{'nas-port'};
$phone = 'N/A';
?>
<?php
echo '<tr>',
'<td class="blockcontentwhite sessionicon">',
'<td class="blockcontentwhite center">' . ("$ip") . '</td>',
'<td class="blockcontentwhite center">' . ("$nas") . '</td>',
'<td class="blockcontentwhite center">' . ("$port") . '</td>',
'<td class="blockcontentwhite center">' . ("$phone") . '</td>',
'</tr>';
endforeach;
?>
所以基本上在第一行的表格中显示了$ ip,$ nas,$ port和$ phone但是它没有显示第二行的第二个值,是什么想法?
谢谢大家
答案 0 :(得分:2)
您正尝试将对象作为数组进行访问。 $info->sessions
的类型为SimpleXMLElement Object
,包含(作为属性)您要在foreach中使用的数组。
将您的foreach(foreach ($info->sessions as $sessions):
)更改为foreach ($info->sessions->session as $sessions):
并:
$count = $sessions->{'session-count'};
$ip = $sessions->session->{'ip-address'};
$nas = $sessions->session->{'nas-ip-address'};
$port = $sessions->session->{'nas-port'};
$phone = 'N/A';
为:
$count = $info->sessions->{'session-count'};
$ip = $sessions->{'ip-address'};
$nas = $sessions->{'nas-ip-address'};
$port = $sessions->{'nas-port'};
$phone = 'N/A';
答案 1 :(得分:0)
我认为对于你提出的问题,Jueecy是正确的。
但我注意到除此之外看起来可能是一个问题。看起来代码会生成多个表,但我想你只想在同一个表中有多行。
我建议将你的foreach移动到生成行的位置:
<?php
$info = simplexml_load_file("https://api.website.co.za/ACCESS_GetAccountSessions?");
echo "<ul info>";
?>
<input type="hidden" name="ctl00$ctl00$contentDefault$contentControlPanel$hdnIsSecure" id="ctl00_ctl00_contentDefault_contentControlPanel_hdnIsSecure" value="false" />
<table id="active_sessions_table" class="blocktable centered" cellpadding="0" cellspacing="0">
<tr class="blockheader">
<td id="active_sessions_title" class="left" colspan="2">
<label class="floatleft">Current connections</label></td>
</tr>
<tr id="trconnections">
<td class="blockcellnopadding" colspan="2">
<table cellpadding="4" cellspacing="0" width="100%" style="border-bottom:solid 1px #bfbfbf;">
<tr>
<td class="columntitle center"> </td>
<td class="columntitle center">IP Address</td>
<td class="columntitle center">NAS IP Address</td>
<td class="columntitle center">Line Port</td>
<td class="columntitle center">Telephone Number</td>
</tr>
<?php
foreach ($info->sessions->session as $session) {
$ip = $session->{'ip-address'};
$nas = $session->{'nas-ip-address'};
$port = $session->{'nas-port'};
$phone = 'N/A';
echo '<tr>',
'<td class="blockcontentwhite sessionicon">',
'<td class="blockcontentwhite center">' . ("$ip") . '</td>',
'<td class="blockcontentwhite center">' . ("$nas") . '</td>',
'<td class="blockcontentwhite center">' . ("$port") . '</td>',
'<td class="blockcontentwhite center">' . ("$phone") . '</td>',
'</tr>';
};
?>