Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 对for循环中的重复元素进行分组和求和_Php_Loops - Fatal编程技术网

Php 对for循环中的重复元素进行分组和求和

Php 对for循环中的重复元素进行分组和求和,php,loops,Php,Loops,我有这样一个for循环: for((int) $i = 0; $i < $number_of_updates; $i++) { //query here $result = mysql_query($query); list($name) = mysql_fetch_row($result); $points = 1; //some functions to execute here... and the

我有这样一个for循环:

for((int) $i = 0; $i < $number_of_updates; $i++) 
{

        //query here
        $result = mysql_query($query);
        list($name) = mysql_fetch_row($result);
        $points = 1;

        //some functions to execute here... and then

        //store results in session to show on other page
        $output = $points.' times '.$name;
        $_SESSION['item'][] = $output;

}
((int)$i=0;$i<$u更新次数;$i++)的

{
//在此查询
$result=mysql\u query($query);
list($name)=mysql\u fetch\u行($result);
$points=1;
//在这里执行一些函数…然后
//在会话中存储结果以显示在其他页面上
$output=$points.'times'.$name;
$\会话['item'][]=$output;
}
然后我在查看页面上显示我的结果,如下所示:

foreach (array_unique($_SESSION['item']) as $output) 
{
    echo ('<p>'.$output.'</p>'); 
}
foreach(数组\u唯一($\u会话['item'])作为$output)
{
回声(“”.$output.“

”); }
它在循环中重复我的结果,如下所示:

for((int) $i = 0; $i < $number_of_updates; $i++) 
{

        //query here
        $result = mysql_query($query);
        list($name) = mysql_fetch_row($result);
        $points = 1;

        //some functions to execute here... and then

        //store results in session to show on other page
        $output = $points.' times '.$name;
        $_SESSION['item'][] = $output;

}
1倍富

1倍富

1倍吧

1倍富

等等

现在是问题。我如何总结这些结果,使它们不会重复?相反,它们是这样显示的:

三倍福


1倍条形图

用户
数组\u计数\u值

for(/*(int) <- this is not neccessary (c++ background?) */ $i = 0; $i < $number_of_updates; $i++) 
{
    // your code
    $_SESSION['item'][] = $name;

}

foreach(array_count_values($_SESSION['item']) as $name => $times)
    echo ('<p>'.$times.' times '.$name.'</p>');
(/*(int)$次)的

echo(“”.$times.“$name.”

”);

当然,直接计算值更节省内存和时间。仅当您需要以某种方式保留元素的顺序时,才需要此版本。

用户
array\u count\u value

for(/*(int) <- this is not neccessary (c++ background?) */ $i = 0; $i < $number_of_updates; $i++) 
{
    // your code
    $_SESSION['item'][] = $name;

}

foreach(array_count_values($_SESSION['item']) as $name => $times)
    echo ('<p>'.$times.' times '.$name.'</p>');
(/*(int)$次)的

echo(“”.$times.“$name.”

”);

当然,直接计算值更节省内存和时间。只有在需要保留元素顺序的情况下,才需要此版本。

为什么不直接将总和存储在数组中

for((int) $i = 0; $i < $number_of_updates; $i++) 
{

    //query here
    $result = mysql_query($query);
    list($name) = mysql_fetch_row($result);
    $points = 1;

    //some functions to execute here... and then

    //store results in session to show on other page
    $_SESSION['item'][$name] +=$points;
}
((int)$i=0;$i<$u更新次数;$i++)的

{
//在此查询
$result=mysql\u query($query);
list($name)=mysql\u fetch\u行($result);
$points=1;
//在这里执行一些函数…然后
//在会话中存储结果以显示在其他页面上
$\会话['item'][$name]+=$points;
}

为什么不直接将总和存储在数组中

for((int) $i = 0; $i < $number_of_updates; $i++) 
{

    //query here
    $result = mysql_query($query);
    list($name) = mysql_fetch_row($result);
    $points = 1;

    //some functions to execute here... and then

    //store results in session to show on other page
    $_SESSION['item'][$name] +=$points;
}
((int)$i=0;$i<$u更新次数;$i++)的

{
//在此查询
$result=mysql\u query($query);
list($name)=mysql\u fetch\u行($result);
$points=1;
//在这里执行一些函数…然后
//在会话中存储结果以显示在其他页面上
$\会话['item'][$name]+=$points;
}
试试这个:

for((int) $i = 0; $i < $number_of_updates; $i++) 
{

        //query here
        $result = mysql_query($query);
        list($name) = mysql_fetch_row($result);
        $points = 1;

        //some functions to execute here... and then

        //store results in session to show on other page
        //$output = $points.' times '.$name;
        //$_SESSION['item'][] = $output;

        if( isset( $_SESSION['count'][$name]  )){
            $prev   = $_SESSION['count'][$name];
        }else{
            $prev   = 0;    
        }

        $_SESSION['count'][$name] = $points + $prev ;
}

print_r( $_SESSION['count'] );
((int)$i=0;$i<$u更新次数;$i++)的

{
//在此查询
$result=mysql\u query($query);
list($name)=mysql\u fetch\u行($result);
$points=1;
//在这里执行一些函数…然后
//在会话中存储结果以显示在其他页面上
//$output=$points.'times'.$name;
//$\会话['item'][]=$output;
if(isset($\会话['count'][$name])){
$prev=$\会话['count'][$name];
}否则{
$prev=0;
}
$\会话['count'][$name]=$points+$prev;
}
打印($_会话['count']);
试试这个:

for((int) $i = 0; $i < $number_of_updates; $i++) 
{

        //query here
        $result = mysql_query($query);
        list($name) = mysql_fetch_row($result);
        $points = 1;

        //some functions to execute here... and then

        //store results in session to show on other page
        //$output = $points.' times '.$name;
        //$_SESSION['item'][] = $output;

        if( isset( $_SESSION['count'][$name]  )){
            $prev   = $_SESSION['count'][$name];
        }else{
            $prev   = 0;    
        }

        $_SESSION['count'][$name] = $points + $prev ;
}

print_r( $_SESSION['count'] );
((int)$i=0;$i<$u更新次数;$i++)的

{
//在此查询
$result=mysql\u query($query);
list($name)=mysql\u fetch\u行($result);
$points=1;
//在这里执行一些函数…然后
//在会话中存储结果以显示在其他页面上
//$output=$points.'times'.$name;
//$\会话['item'][]=$output;
if(isset($\会话['count'][$name])){
$prev=$\会话['count'][$name];
}否则{
$prev=0;
}
$\会话['count'][$name]=$points+$prev;
}
打印($_会话['count']);

PSA:mysql*函数是。不建议编写新代码,因为这样会阻止您将来升级。相反,使用or和.PSA:mysql的
。不建议编写新代码,因为这样会阻止您将来升级。相反,请使用或和。您可以回送$\u会话['item']:
foreach($\u会话['item']作为$name=>$times){echo(“$times$name

”);}
您可以回送$\u会话['item']:
foreach($\u会话['item']作为$name=>$times){echo($times$name

”);