Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.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/8/qt/7.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 使用从另一个数组筛选的数组_sum()_Php_Arrays - Fatal编程技术网

Php 使用从另一个数组筛选的数组_sum()

Php 使用从另一个数组筛选的数组_sum(),php,arrays,Php,Arrays,如何array\u sumalldebit值或allcredit值 预期结果: 借方=20+60=80 积分=30+40+50=120 我尝试过过滤它,然后使用数组_sum,但不起作用 $type=array("debit", "credit", "credit", "credit", "debit"); $amount=array(20,30,40,50,60); 你不可能通过这种方式得到总数,因为你实际上没有对数量数组做任何事情,但你已经非常接近了 您可以使用array\u inters

如何
array\u sum
all
debit
值或all
credit

预期结果: 借方=20+60=80 积分=30+40+50=120

我尝试过过滤它,然后使用数组_sum,但不起作用

$type=array("debit", "credit", "credit", "credit", "debit");

$amount=array(20,30,40,50,60);

你不可能通过这种方式得到总数,因为你实际上没有对数量数组做任何事情,但你已经非常接近了

您可以使用
array\u intersect\u key
查找
$amount
中与筛选的
$types
数组中的键匹配的条目

function filterDebit($finddebit){
    return ($finddebit == "debit");
}
$finaldebit = array_filter($type, 'filterDebit');
echo array_sum($finaldebit);


如果可能的话,我认为最好在早些时候解决这个问题,这样两组关联的值就不会出现在这样的单独数组中,特别是如果这两个数组来自一个数据库,这将能够更有效地处理这个过滤/聚合,但是因为我不知道数组的来源,对此我没有什么建议。

你不可能通过这种方式得到总数,因为你实际上没有对数量数组做任何事情,但你已经非常接近了

您可以使用
array\u intersect\u key
查找
$amount
中与筛选的
$types
数组中的键匹配的条目

function filterDebit($finddebit){
    return ($finddebit == "debit");
}
$finaldebit = array_filter($type, 'filterDebit');
echo array_sum($finaldebit);

如果可能的话,我认为最好在早些时候解决这个问题,这样两组关联的值就不会出现在这样的单独数组中,特别是如果这两个数组来自一个数据库,这将能够更有效地处理这个过滤/聚合,但是因为我不知道数组的来源,对此我没有什么建议。

你可以选择用户的答案,也可以用for循环手工完成

echo array_sum(array_intersect_key($amount, $finaldebit));
函数sumType($type,$amount,$key){
$sum=0;
对于($i=0;$i
您可以选择用户的答案,也可以使用for循环手动完成

echo array_sum(array_intersect_key($amount, $finaldebit));
函数sumType($type,$amount,$key){
$sum=0;
对于($i=0;$i
只需这样做,无需任何复杂逻辑:(解释内联)

打印:

$type = array('debit', 'credit', 'credit', 'credit', 'debit');

$amount = array(20, 30, 40, 50, 60);

$sum = array_fill_keys(array_unique($type), 0);  // initialize the empty associative array with possible keys from $type array

foreach ($type as $key => $t) {
    $sum[$t] += $amount[$key]; // Sum up the values based on keys
}

print_r($sum);


注意:确保两个数组都具有正确的值。由于两者不同,如果值不匹配,可能会有一些差异

只需这样做,无需任何复杂的逻辑:(解释内联)

打印:

$type = array('debit', 'credit', 'credit', 'credit', 'debit');

$amount = array(20, 30, 40, 50, 60);

$sum = array_fill_keys(array_unique($type), 0);  // initialize the empty associative array with possible keys from $type array

foreach ($type as $key => $t) {
    $sum[$t] += $amount[$key]; // Sum up the values based on keys
}

print_r($sum);


注意:确保两个数组都具有正确的值。由于两者不同,如果值不匹配,可能会有一些差异

@不要惊慌更具体,但你也可以这样做

Array
(
    [debit] => 80
    [credit] => 120
)

不要惊慌更为具体,但你也可以这样做

Array
(
    [debit] => 80
    [credit] => 120
)

这里有一种可能很复杂的方法,不仅适用于借记,而且适用于
$type
中的任何唯一值:

<?php

$type=array("debit", "credit", "credit", "credit", "debit");

$amount=array(20,30,40,50,60);

function filter_price($types, $amount, $type){
    if($type == 'debit'){
        $key = 'debit';
    }else{
        $key = 'credit';
    }
    $sum = 0;
    foreach($types as $index->$type){
        if($type == $key){
            $sum += $amount[$index];
        }
    }
    return $sum;
}
echo filter_price($type, $amount, 'debit');
echo filter_price($type, $amount, 'credit');

这里有一种可能很复杂的方法,不仅适用于借记,而且适用于
$type
中的任何唯一值:

<?php

$type=array("debit", "credit", "credit", "credit", "debit");

$amount=array(20,30,40,50,60);

function filter_price($types, $amount, $type){
    if($type == 'debit'){
        $key = 'debit';
    }else{
        $key = 'credit';
    }
    $sum = 0;
    foreach($types as $index->$type){
        if($type == $key){
            $sum += $amount[$index];
        }
    }
    return $sum;
}
echo filter_price($type, $amount, 'debit');
echo filter_price($type, $amount, 'credit');

它必须是
array\u sum
?这可能是foreach吗?@unata Sanatarai foreach没有问题。重要的是最终结果。:)这两个数组是分开的吗?@EatPeanutButter:是的。它们是单独的数组。它必须是
数组\u sum
?这可能是foreach吗?@unata Sanatarai foreach没有问题。重要的是最终结果。:)这两个数组是分开的吗?@EatPeanutButter:是的。它们是独立阵列。我喜欢这个概念。@Thamilan的答案也是一个很好的选择:我喜欢这个概念。@Thamilan的答案也是一个很好的选择: