I have a text file there I store all the data like this:
dept|test-1|365|0
dept|test-2|134|0
expense|test-3|4387|0
expense|test-4|105|0
How can I get the sum of each category for all the transaction within that category?
In this example, the category dept
will have a total sum of 499
and the category expense
will have a total sum of 4'492
.
But how can I do this within PHP?
Here's my current code:
function do_maths($expression) {
eval('$o = ' . preg_replace('/[^0-9\+\-\*\/\(\)\.]/', '', $expression) . ';');
return $o;
}
function inifile($fname, $word) {
$contents = file_get_contents($fname.'.ini');
$pattern = preg_quote($word, '/');
$pattern = "/^.*$pattern.*\$/m";
if(preg_match_all($pattern, $contents, $matches)) {
sort($matches[0]);
# TABELL
echo '<div class="table">';
# LOOP
foreach($matches[0] AS $found) {
$transaction = explode('|', $found);
echo '<div class="table-row'.($transaction[3] == 1 ? ' color-grey-light" title="'.$transaction[1].' är betalad"' : '"').'>';
echo '<div class="table-cell-left table-cell-left-width">';
echo $transaction[1];
echo '</div>';
echo '<div class="table-cell-right">';
echo '<span class="sum">';
echo do_maths($transaction[2]);
echo '</span> kr';
echo '</div>';
echo '</div>';
}
echo '</div>';
} else {
echo '<div class="message color-blue">';
echo 'Kunde inte hitta något';
echo '</div>';
}
}
This should work for you:
First read your text file into an array with
file()
. Then go through each array element(each line of your text file) andexplode()
it by|
as delimiter.So your array will change from (Array at point 1):
to (Array at point 2):
After this you can loop through your
$lines
array and use the category (first array element of the subArray) as index and add the value (third array element) to the array.Then you can call
array_sum()
for each category subArray witharray_map()
.So you sum all values together for each category. So your array (Array at point 3):
Will become like this (Array at point 4):
And at the end just loop through your array and display your data:
output:
EDIT:
So my code implemented into your function will look something like this:
The first part is the same as in my code above. But to now only loop through the data of the category which you want we need the keys to these subArrays. Which we get with this line:
So basically we grab all categories from your array with
array_column($lines, 0)
, which will return an array like this:Then with
array_keys()
and$category
we only get the keys of this array from the category which you want. Which for example forexpense
will be:Then we just check if we have some keys, means some lines with the category which you want. If yes we loop through these keys and use them to access the data, which you want.
After the foreach loop to print the sum of the values we use this line:
But let's break it a bit down. First we have:
Where we grab the key intersect of these two arrays with
array_intersect_key()
. On the left side we have the array$lines
as first array which as we know looks something like this:Now on the other side as second array we have the keys from the category which we want, so:
And then the result/intersect will be (Note: Since we grab the intersect of they keys, we have to flip
$keys
witharray_flip()
):Out of this array we get the second column from each subArray with
array_column()
. So from the above array we will end up with:And then finally we get the sum of this array with
array_sum()
and you will get: