Php 在最后一个foreach上只打印一次

Php 在最后一个foreach上只打印一次,php,foreach,echo,Php,Foreach,Echo,我有这个密码 $servers = array( array( 'type' => 'tf2', 'host' => '193.192.59.191:27015', ), array( 'type' => 'tf2', 'host' => '193.192.59.191:27025', ),

我有这个密码

$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>");
    }
$servers=array(
排列(
'type'=>'tf2',
“主机”=>“193.192.59.191:27015”,
),
排列(
'type'=>'tf2',
“主机”=>“193.192.59.191:27025”,
),
排列(
'type'=>'tf2',
“主机”=>“193.192.59.191:27035”,
),
排列(
'type'=>'tf2',
“主机”=>“193.192.59.191:27045”,
),
排列(
'type'=>'tf2',
“主机”=>“193.192.59.191:27055”,
),
排列(
'type'=>'tf2',
“主机”=>“193.192.59.191:27065”,
),
排列(
'type'=>'tf2',
“主机”=>“193.192.59.191:27075”,
),
);
$totalcount=0;
函数打印结果($results){
foreach($id=>$data的结果){
打印表格(数据);
}
}
函数打印表($data){
全球美元总数;
$totalcount=$totalcount+$data['gq_MaxPlayer'];
echo$totalcount;
}
如果(!$data['gq_online']){
}
printf(“”);
}

我希望它只在最后一次打印_表时回显,所以它只显示一次(在最后一次执行函数时)。而且不是每次它都能起作用。我该怎么做?谢谢

这意味着在上一次迭代中调用print_表?然后尝试:

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…)?因此,您可以将$curr变量替换为$id…。

您可以不使用完整数组,而只使用函数的最后一个元素,如下所示:

function print_result( $result ) {
  $end = end( $result );
  if ( $end !== false ) print_table( $end );
  reset ( $result ); // to recovery internal pointer
}

我不认为你粘贴了全部代码。有丢失的呼叫..我想做echo$totalcount;仅在最后一次调用print_result时。