Php 在另一个值的顶部回显值

Php 在另一个值的顶部回显值,php,Php,我认为这很简单,但我似乎没有完成。我想做的是将从FOREACH循环中得到的值显示在其他回显值的顶部 function writeMsg($total) { echo $total. "< This must display First"; } foreach ($array as $value) { echo $value["Price"]."<br>"; $total = $value["Total"]; } writeMsg($total); 以前 ec

我认为这很简单,但我似乎没有完成。我想做的是将从FOREACH循环中得到的值显示在其他回显值的顶部

function writeMsg($total) {
echo $total. "< This must display First";
}

foreach ($array as $value) {
   echo $value["Price"]."<br>";
   $total = $value["Total"];
}

writeMsg($total);
以前

 echo $value["Price"]."<br>";
echo$value[“Price”]。“
”;

我希望你们能理解我的问题

我不完全确定你想得到什么,但我的猜测是:

function writeMsg($total) {
echo $total. "< This must display First";
}
$total = 0;
$prices = array();

foreach ($array as $value){
    $prices [] = $value["Price"];
    $total += $value["Total"];
}

echo implode("<br>", $prices);
echo "<br>";
writeMsg($total);
函数writeMsg($total){
echo$total.“<必须首先显示”;
}
$total=0;
$prices=array();
foreach($array作为$value){
$prices[]=$value[“Price”];
$total+=$value[“total”];
}
回声内爆(“
”,美元价格); 回声“
”; 书面形式(共计);
您可以使用:

ob_start();//现在回显的所有内容都已缓冲
foreach($array作为$value){
echo$value[“价格”]。“
”; $total=$value[“total”]; } $all_the_echoos=ob_get_clean();//捕获缓冲区到变量 writeMsg(总计);//呼应总 echo$all__回音;//回显捕获的缓冲区

请注意,可能有一个更干净的解决方案,但除非你更新你的问题,否则我只能猜测你想要实现什么。

我认为你想要的是这样的。在foreach循环中,您希望积累数据,并在以后将其回显出来。在那里,您可以选择首先回显什么

function writeMsg($total) {
    echo $total. "< This must display First";
}


$total = 0;
$prices = '';

foreach ($array as $value){
    // concatenate all prices into a single string. echo this later (after echo of totals)
    $prices .= $value["Price"]."<br>";

    // sum up total of all values
    $total += $value["Total"];
}

writeMsg($total);
echo $prices;
函数writeMsg($total){
echo$total.“<必须首先显示”;
}
$total=0;
$价格='';
foreach($array作为$value){
//将所有价格连接成一个字符串。稍后回显(在回显总计之后)
$prices.=$value[“Price”]。“
”; //所有值的总和 $total+=$value[“total”]; } 书面形式(共计); 回声美元价格;
您在foreach中回音。如果你不想在其他输出之前显示该输出,请不要在foreach中回音。不,你的问题不清楚。所以你想回音$total,对吗?谢谢,伙计,这正是我要找的。
ob_start(); // everything echo'ed now is buffered
foreach ($array as $value){
    echo $value["Price"]."<br>";
    $total = $value["Total"];
}
$all_the_echoes = ob_get_clean(); // capture buffer to variable

writeMsg($total); // echoes the total
echo $all_the_echoes; // echoes the captured buffer
function writeMsg($total) {
    echo $total. "< This must display First";
}


$total = 0;
$prices = '';

foreach ($array as $value){
    // concatenate all prices into a single string. echo this later (after echo of totals)
    $prices .= $value["Price"]."<br>";

    // sum up total of all values
    $total += $value["Total"];
}

writeMsg($total);
echo $prices;