开发者

PHP - While loop

print "<ul>";
foreach ($arr as $value) {
    echo("<li>" . $value[storeid] . " " . (开发者_Go百科$value[dvdstock] + $value[vhsstock]) . "</li>");
}
print "</ul>";

Will output

•2 20
•2 10
•1 20
•1 20
•1 10

I was wondering how I would adapt this loop so it outputs the total values for each &value[storeid]

•1 50
•2 30

Thanks very much :)


Use another array to calculate the values you want:

// 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>";

Demo Link


If you are getting the data from an SQL database then you should do this using SUM() functions in the SQL as it is more efficient. If the data source is from somewhere else you should do something like this:

//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>";

Demo Link


It is a for loop and you have to make two of them. First to compute the sum and then to iterate over these values:

$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>";

Demo Link

Btw one word about strings:
Either use single quotes ' with concatenation .: 'foo: ' . $bar.
Or double quotes " and put the variables inside the string: "foo: $bar".


<?php
$sums = array();
foreach ($arr as $value)
{
    $sums[$value['storeid']] += $value['dvdstock'];
}
print_r($sums);
?>


Correct version:

<?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);

?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜