我收到了这段代码
$servers = array(
array(
'type' => 'tf2',
'host' => '193.192.59.191:27015',
),
array(
'type' => 'tf2',
'host' => '193.192.59.191:27025',
),
array(
'type' => 'tf2',
'host' => '193.192.59.191:27035',
),
array(
'type' => 'tf2',
'host' => '193.192.59.191:27045',
),
array(
'type' => 'tf2',
'host' => '193.192.59.191:27055',
),
array(
'type' => 'tf2',
'host' => '193.192.59.191:27065',
),
array(
'type' => 'tf2',
'host' => '193.192.59.191:27075',
),
);
$totalcount = 0;
function print_results($results) {
foreach ($results as $id => $data) {
print_table($data);
}
}
function print_table($data) {
global $totalcount;
$totalcount = $totalcount + $data['gq_maxplayers'];
echo $totalcount;
}
if (!$data['gq_online']) {
}
printf("</tr></tbody>");
}
我希望它只在最后一次print_table时回显,所以它只会显示一次(在它上次执行该功能时)。而不是每次它都发挥作用。我该怎么办?谢谢!
答案 0 :(得分:1)
你可能没有itarate完整数组,但只采用end()函数的最后一个元素,如下所示:
function print_result( $result ) {
$end = end( $result );
if ( $end !== false ) print_table( $end );
reset ( $result ); // to recovery internal pointer
}
答案 1 :(得分:-1)
这意味着在最后一次迭代中调用print_table?然后尝试:
function print_results($results) {
$total = count($results);
$curr = 0;
foreach ($results as $id => $data) {
if ($curr == $total - 1) print_table($data);
$curr++;
}
}
它还取决于$ id值。 $ id值是相关的(0,1,2,3 ......)?所以你可以用$ id替换$ curr变量......