Php 计算文本文件中每个类别的总和
我在那里有一个文本文件,我存储的所有数据如下:Php 计算文本文件中每个类别的总和,php,file,text,sum,file-get-contents,Php,File,Text,Sum,File Get Contents,我在那里有一个文本文件,我存储的所有数据如下: dept|test-1|365|0 dept|test-2|134|0 expense|test-3|4387|0 expense|test-4|105|0 <?php function getCategoryFromFile($fileName, $category) { $lines = file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)
dept|test-1|365|0
dept|test-2|134|0
expense|test-3|4387|0
expense|test-4|105|0
<?php
function getCategoryFromFile($fileName, $category) {
$lines = file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$lines = array_map(function($v){
return explode("|", $v);
}, $lines);
$keys = array_keys(array_column($lines, 0), $category);
if(!empty($keys)) {
echo '<div class="table">';
foreach($keys as $key) {
echo '<div class="table-row' . ($lines[$key][3] == 1 ? ' color-grey-light" title="' . $lines[$key][1] . ' är betalad"' : '"') . '>';
echo '<div class="table-cell-left table-cell-left-width">';
echo $lines[$key][1];
echo '</div>';
echo '</div>';
}
echo '<div class="table-cell-right">';
echo '<span class="sum">';
echo array_sum(array_column(array_intersect_key($lines, array_flip($keys)), 2));
echo '</span> kr';
echo '</div>';
echo '</div>';
} else {
echo '<div class="message color-blue">';
echo 'Kunde inte hitta något';
echo '</div>';
}
}
//As example
getCategoryFromFile("test.txt", "expense");
?>
Array
(
[0] => dept
[1] => dept
[2] => expense
[3] => expense
)
Array
(
[0] => Array
(
[0] => dept
[1] => test-1
[2] => 365
[3] => 0
)
//...
)
如何获得该类别内所有交易的每个类别的总和
在本例中,类别部门
的总和为499
,类别费用
的总和为4'492
但是如何在PHP中实现这一点呢
以下是我当前的代码:
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>';
}
}
函数do\u数学($expression){
eval(“$o=”.preg\u replace(“/[^0-9\+\-\*\/\(\)\.]/”,“$expression)。”;”);
退还$o;
}
函数文件($fname,$word){
$contents=file_get_contents($fname..ini');
$pattern=preg_quote($word,“/”);
$pattern=“/^.*$pattern.*\$/m”;
if(preg_match_all($pattern、$contents、$matches)){
排序($matches[0]);
#塔贝尔
回声';
#环路
foreach($found将[0]匹配为$found){
$transaction=explode(“|”,$found);
echo'这应该适合您:
首先用将文本文件读入数组。然后遍历每个数组元素(文本文件的每一行),并以
作为分隔符
因此,您的数组将从(点1处的数组)更改为:
到(点2处的阵列):
在此之后,您可以在$lines
数组中循环,使用类别(子数组的第一个数组元素)作为索引,并将值(第三个数组元素)添加到数组中
然后可以使用调用每个类别子数组
因此,将每个类别的所有值相加。因此,您的数组(第3点处的数组):
将变成这样(点4处的数组):
最后,只需在数组中循环并显示数据:
<?php
$lines = file("test.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); //Array at point 1
$lines = array_map(function($v){
return explode("|", $v);
}, $lines); //Array at point 2
foreach($lines as $line)
$data[$line[0]][] = $line[2];
//Array at point 3
$result = array_map("array_sum", $data);
//Array at point 4
foreach($result as $category => $sum)
echo $category . " = " . $sum . "<br>";
?>
编辑:
因此,我在函数中实现的代码如下所示:
dept|test-1|365|0
dept|test-2|134|0
expense|test-3|4387|0
expense|test-4|105|0
<?php
function getCategoryFromFile($fileName, $category) {
$lines = file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$lines = array_map(function($v){
return explode("|", $v);
}, $lines);
$keys = array_keys(array_column($lines, 0), $category);
if(!empty($keys)) {
echo '<div class="table">';
foreach($keys as $key) {
echo '<div class="table-row' . ($lines[$key][3] == 1 ? ' color-grey-light" title="' . $lines[$key][1] . ' är betalad"' : '"') . '>';
echo '<div class="table-cell-left table-cell-left-width">';
echo $lines[$key][1];
echo '</div>';
echo '</div>';
}
echo '<div class="table-cell-right">';
echo '<span class="sum">';
echo array_sum(array_column(array_intersect_key($lines, array_flip($keys)), 2));
echo '</span> kr';
echo '</div>';
echo '</div>';
} else {
echo '<div class="message color-blue">';
echo 'Kunde inte hitta något';
echo '</div>';
}
}
//As example
getCategoryFromFile("test.txt", "expense");
?>
Array
(
[0] => dept
[1] => dept
[2] => expense
[3] => expense
)
Array
(
[0] => Array
(
[0] => dept
[1] => test-1
[2] => 365
[3] => 0
)
//...
)
因此,基本上我们使用array\u列($lines,0)
从数组中获取所有类别,该列将返回如下数组:
dept|test-1|365|0
dept|test-2|134|0
expense|test-3|4387|0
expense|test-4|105|0
<?php
function getCategoryFromFile($fileName, $category) {
$lines = file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$lines = array_map(function($v){
return explode("|", $v);
}, $lines);
$keys = array_keys(array_column($lines, 0), $category);
if(!empty($keys)) {
echo '<div class="table">';
foreach($keys as $key) {
echo '<div class="table-row' . ($lines[$key][3] == 1 ? ' color-grey-light" title="' . $lines[$key][1] . ' är betalad"' : '"') . '>';
echo '<div class="table-cell-left table-cell-left-width">';
echo $lines[$key][1];
echo '</div>';
echo '</div>';
}
echo '<div class="table-cell-right">';
echo '<span class="sum">';
echo array_sum(array_column(array_intersect_key($lines, array_flip($keys)), 2));
echo '</span> kr';
echo '</div>';
echo '</div>';
} else {
echo '<div class="message color-blue">';
echo 'Kunde inte hitta något';
echo '</div>';
}
}
//As example
getCategoryFromFile("test.txt", "expense");
?>
Array
(
[0] => dept
[1] => dept
[2] => expense
[3] => expense
)
Array
(
[0] => Array
(
[0] => dept
[1] => test-1
[2] => 365
[3] => 0
)
//...
)
然后,我们只需检查是否有一些键,表示您想要的类别的一些行。如果有,我们循环通过这些键并使用它们访问您想要的数据
在foreach循环之后,我们使用此行打印值的总和:
$keys = array_keys(array_column($lines, 0), $category);
echo array_sum(array_column(array_intersect_key($lines, array_flip($keys)), 2));
但是让我们把它分解一下。首先我们有:
array_intersect_key($lines, array_flip($keys))
在这里,我们获取这两个数组的键相交。在左侧,我们将数组$lines
作为第一个数组,正如我们所知,它看起来像这样:
dept|test-1|365|0
dept|test-2|134|0
expense|test-3|4387|0
expense|test-4|105|0
<?php
function getCategoryFromFile($fileName, $category) {
$lines = file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$lines = array_map(function($v){
return explode("|", $v);
}, $lines);
$keys = array_keys(array_column($lines, 0), $category);
if(!empty($keys)) {
echo '<div class="table">';
foreach($keys as $key) {
echo '<div class="table-row' . ($lines[$key][3] == 1 ? ' color-grey-light" title="' . $lines[$key][1] . ' är betalad"' : '"') . '>';
echo '<div class="table-cell-left table-cell-left-width">';
echo $lines[$key][1];
echo '</div>';
echo '</div>';
}
echo '<div class="table-cell-right">';
echo '<span class="sum">';
echo array_sum(array_column(array_intersect_key($lines, array_flip($keys)), 2));
echo '</span> kr';
echo '</div>';
echo '</div>';
} else {
echo '<div class="message color-blue">';
echo 'Kunde inte hitta något';
echo '</div>';
}
}
//As example
getCategoryFromFile("test.txt", "expense");
?>
Array
(
[0] => dept
[1] => dept
[2] => expense
[3] => expense
)
Array
(
[0] => Array
(
[0] => dept
[1] => test-1
[2] => 365
[3] => 0
)
//...
)
现在在第二个数组的另一侧,我们有我们想要的类别的键,因此:
Array
(
[0] => 2
[1] => 3
)
从这个数组中,我们得到每个子数组的第二列。因此,从上面的数组中,我们将得到:
Array
(
[0] => 4387
[1] => 105
)
你试过什么了吗?你现在打开/阅读文本文件的地方有代码吗?我已经在我的问题中添加了全部代码:)很抱歉搞混了。我想我累了--时钟现在是00:47,工作得很好!但是我怎么能把你的代码放进我的代码中呢?我坐在这里,头上有一个大大的问号,我的眼睛都快睁不开了在过去的7分钟内,我已经这样做了。@ErikEdgren 1)因此,首先,自从我的第一篇帖子我在我的答案中添加了更多的细节,我希望这有助于更好地理解我的代码,并且你知道你的do_math()
函数发生了什么2)我可以建议你看看这个:3)我没有真正看透你的ini文件()
函数,它应该做什么?你想对你的模式做什么?如果我们谈论的是文本文件,为什么你会得到.ini
文件的内容?谢谢你的评论。1)我已经阅读了你在回答中的上一次编辑,但我想我已经厌倦了理解其中的一半。2)谢谢你的链接。我会阅读它3)MyInFile()
函数搜索您为$word
键入的类别,并打印该类别的所有事务category@ErikEdgren ( 1)如果你不理解它并陷入困境,请先睡觉,然后重新看一眼)2)现在我知道它应该做什么了。只是我不明白为什么*.ini
?我以为我们在谈论一个文本文件?!@ErikEdgren不客气。给我一分钟时间,我会对我的答案进行编辑。