Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.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

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-While循环_Php_Loops_While Loop - Fatal编程技术网

PHP-While循环

PHP-While循环,php,loops,while-loop,Php,Loops,While Loop,我想知道如何调整这个循环,以便它输出每个&value[storeid]的总值 •2 20 •2 10 •1 20 •1 20 •1 10 非常感谢:)正确版本: •1 50 •2 30 使用另一个数组来计算所需的值: <?php $initial = array ( array ( 'id' => 1, 'amount' => 10 ), array ( 'id' => 1,

我想知道如何调整这个循环,以便它输出每个&value[storeid]的总值

•2 20
•2 10
•1 20
•1 20
•1 10
非常感谢:)

正确版本:

•1 50
•2 30

使用另一个数组来计算所需的值:

<?php

$initial = array (
    array (
        'id'     => 1,
        'amount' => 10
    ),
    array (
        'id'     => 1,
        'amount' => 10
    ),
    array (
        'id'     => 2,
        'amount' => 20
    ),
    array (
        'id'     => 2,
        'amount' => 20
    ),
    array (
        'id'     => 2,
        'amount' => 20
    ),
);

$result = array ();

foreach ($initial as $value) {
    $result[$value['id']] += $value['amount'];
}

print_r($result);

?>
//设置一个快速存储数据的位置
$stores=array();
foreach($arr作为$value){
如果(!isset($stores[$value['storeid']]){//init检查需要避免通知
$stores[$value['storeid']=$value['dvdstock']+$value['vhsstock'];
}否则{
$stores[$value['storeid']+=$value['dvdstock']+$value['vhsstock'];
}
}
ksort($stores);//按存储ID排序ASC
打印“
    ”; //循环浏览新数据 foreach($存储为$id=>$value){ 回声(“
  • ”$id.“($value)。“
  • ”); } 打印“
”;

如果您是从SQL数据库获取数据,则应使用SQL中的
SUM()
函数执行此操作,因为它效率更高。如果数据源来自其他地方,则应执行以下操作:

// setup a quick place to store the data
$stores = array();
foreach ($arr as $value) {
    if(!isset($stores[$value['storeid']])){  // init check required to avoid Notices
        $stores[$value['storeid']] = $value['dvdstock'] + $value['vhsstock'];
    }else{
        $stores[$value['storeid']] += $value['dvdstock'] + $value['vhsstock'];
    }
}
ksort($stores);  // sort by storeid ASC

print "<ul>";
// loop through the new data
foreach ($stores as $id => $value) {
    echo("<li>" . $id . " " . ($value) . "</li>");
}
print "</ul>";
//求和数据
foreach($arr作为$value){
如果(!isset($sums[$value['storeid']]){//init检查需要避免通知
$sums[$value['storeid']=$value['dvdstock']+$value['vhsstock'];
}否则{
$sums[$value['storeid']+=$value['dvdstock']+$value['vhsstock'];
}
}
ksort(合计);//按存储ID排序ASC
打印“
    ”; foreach($key=>$sum的总和){ 回声(“
  • $key$sum
  • ”; } 打印“
”;


这是一个用于循环的
,您必须制作两个循环。首先计算总和,然后迭代这些值:

<?php
$sums = array();
foreach ($arr as $value)
{
    $sums[$value['storeid']] += $value['dvdstock'];
}
print_r($sums);
?>
$data=array();
foreach($arr作为$value){
如果(!isset($data[$value['storeid']]){//init检查需要避免通知
$data[$value['storeid']=$value['dvdstock']+$value['vhsstock'];
}否则{
$data[$value['storeid']+=$value['dvdstock']+$value['vhsstock'];
}    
}
ksort($data);//按存储ID排序ASC
打印“
    ”; foreach($storeid=>$sum形式的数据){ echo(“
  • ”.$storeid.“($sum)。”
  • ); } 打印“
”;

顺便说一句,关于字符串:
使用单引号
'
和串联
'foo:'$条形码


或者双引号
并将变量放入字符串:
“foo:$bar”中“

我怀疑这是否有用,它基本上是将数组从一个数组复制到另一个数组,但使用
+=
WTF,不,它将遍历初始数组,并汇总每个唯一键的总数。我对这个答案投了否决票,因为它没有使用OP的样本输入数据,没有提供所需的输出,生成通知,并且是仅代码应答。如果你能够编辑这个帖子来形成一个独特的有价值的答案,我会考虑删除我的下一个投票,并可能会投票。否则,请考虑删除这个帖子以减少页面膨胀。如果你正在汇总,为什么不选择StuteID、SUM(DVDSCONT+VHSSTORD)作为StuteID ?> GNARF的表SttestObk组的股票:你读过第一个句子吗?如果你从SQL数据库中获取数据,那么你应该使用SUM()来做这件事。SQL中的函数,因为它更有效。你看过我的评论了吗?我读了第一句话,然后说:“为什么他不把dvdstock和vhsstock也加在一起?”另外,我认为使用
SUM()
的SQL查询示例对于OP考虑这个问题可能很有用。@gnarf:我同意这个示例。但是克里斯说了关于如何求和的任何事情,所以他可能和你有相同的想法。但无论如何,这个例子很好:)如果这些值来自数据库,那么您必须相应地更改sql,以便resultset也包含总数。我否决了这个仅代码的答案,因为它没有提供所需的输出,并且会生成通知。如果你能编辑这个答案,形成一个独特的有价值的方法,我会考虑删除我的下票,并可能投票。否则,请考虑删除这个帖子,以减少页面膨胀。谢谢,米克马库萨,但是我认为这个答案很好。我知道代码并不完美,但我认为StackOverflow并不是一个代码站点。我回答中的代码显示了解决方案的概念。它并不意味着要复制粘贴到生产代码中。
//Sum data
foreach ($arr as $value) {
    if (!isset($sums[$value['storeid']])) {  // init check required to avoid Notices
        $sums[$value['storeid']] = $value['dvdstock'] + $value['vhsstock'];
    } else {
        $sums[$value['storeid']] += $value['dvdstock'] + $value['vhsstock'];
    }
}
ksort($sums);  // sort by storeid ASC

print "<ul>";
foreach ($sums as $key => $sum) {
    echo("<li>$key $sum</li>");
}
print "</ul>";
<?php
$sums = array();
foreach ($arr as $value)
{
    $sums[$value['storeid']] += $value['dvdstock'];
}
print_r($sums);
?>
$data = array();

foreach ($arr as $value) {
    if (!isset($data[$value['storeid']])) {  // init check required to avoid Notices
        $data[$value['storeid']] = $value['dvdstock'] + $value['vhsstock'];
    } else {
        $data[$value['storeid']] += $value['dvdstock'] + $value['vhsstock'];
    }    
}
ksort($data);  // sort by storeid ASC

print "<ul>";
foreach ($data as $storeid => $sum) {
    echo('<li>' . $storeid . ' ' . ($sum) . '</li>');
}
print "</ul>";