views:

88

answers:

5
print "<ul>";
foreach ($arr as $value) {
    echo("<li>" . $value[storeid] . " " . ($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 :)

A: 

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

?>
ninuhadida
I doubt this will do anything useful, its basically copying the array from one array to another but using `+=`
gnarf
WTF, no, it would go through the initial array, and sums up the total for each unique key.
ninuhadida
How would taking the array keys from `$initial` and using the same `$key` `+=` the `$value` (which is obviously an array) do anything other than copy the array?
gnarf
sorry, my bad. edited: http://codepad.org/i0ETIeKO
ninuhadida
@ninuhadida: You should change your answer then to make it more correct.
Felix Kling
@Felix: done ;)
ninuhadida
+1  A: 

Use another array to calculate the values you want:

// setup a quick place to store the data
$stores = array();
foreach ($arr as $value) {
  $stores[$value['storeid']] += $value['dvdstock'] + $value['vhsstock'];
}
print "<ul>";
// loop through the new data
foreach ($stores as $id => $value) {
    echo("<li>" . $id . " " . ($value) . "</li>");
}
print "</ul>";
gnarf
+1  A: 

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) {
$sums[{$value[storeid]}] += ($value[dvdstock] + $value[vhsstock]);
}

print "<ul>";
foreach ($sums as $key => $sum) {
    echo("<li>$key $sum</li>");
}
print "</ul>";
Chris
If you were summing anyway, why not `SELECT storeid, SUM(dvdstock+vhsstock) as stock FROM TABLE storestock GROUP BY storeid` ?
gnarf
@gnarf: Have you read the first sentence?: *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*
Felix Kling
Have you read my comment? I read the first sentence, and then said "Why doesn't he add dvdstock and vhsstock together in the query too?" ;) - Also, I thought having an example SQL query that used `SUM()` might be useful to the OP considering the question.
gnarf
@gnarf: I agree with the example. But Chris does say anything about how to sum the values so maybe he had the same in mind as you. But anyway, example is good :)
Felix Kling
A: 
<?php
$sums = array();
foreach ($arr as $value)
{
    $sums[$value['storeid']] += $value['dvdstock'];
}
print_r($sums);
?>
Sjoerd
A: 

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) {
    $data[$value['storeid']] += $value['dvdstock'] + $value['vhsstock'];
}


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

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".

Felix Kling