清除阵列的总数

时间:2014-10-31 19:58:55

标签: php mysql arrays

我有一个数组,其中有一些数字(基于$$$销售额)附加到一个'名称' ' ID'和'目标'你可以看到一些名字,id和目标都是一样的。我的目标是收集总金额,并将每个总额附加到任何名称' id,' id'和'目标'做了这笔交易。我老实说不知道如何解决这个问题,因为我还在学习。

Array ( 
    [0] => Array ( [name] => L.Chane [id] => oper-4bceffd1-21e0af5b [goal] => 2014-10-25000 [amount] => 360.00 ) 
    [1] => Array ( [name] => L.Chane [id] => oper-4bceffd1-21e0af5b [goal] => 2014-10-25000 [amount] => 450.00 ) 
    [2] => Array ( [name] => L.Chane [id] => oper-4bceffd1-21e0af5b [goal] => 2014-10-25000 [amount] => 450.00 ) 
    [3] => Array ( [name] => C.James [id] => oper-4c236420-0b11e945 [goal] => 2014-10-25000 [amount] => 370.00 ) 
    [4] => Array ( [name] => C.James [id] => oper-4c236420-0b11e945 [goal] => 2014-10-25000 [amount] => 175.00 ) 
    [5] => Array ( [name] => C.James [id] => oper-4c236420-0b11e945 [goal] => 2014-10-25000 [amount] => 155.00 )
    [6] => Array ( [name] => K.Crass [id] => oper-4c597644-402490ee [goal] => 2014-10-25000 [amount] => 200.00 ) 
    [7] => Array ( [name] => K.Crass [id] => oper-4c597644-402490ee [goal] => 2014-10-25000 [amount] => 599.00 ) 
    [8] => Array ( [name] => K.Crass [id] => oper-4c597644-402490ee [goal] => 2014-10-25000 [amount] => 50.00 ) 
    [9] => Array ( [name] => R.Cervantes [id] => oper-4f05a90b-03b379f9 [goal] => 2014-10-25000 [amount] => 450.00 ) 
   [10] => Array ( [name] => R.Cervantes [id] => oper-4f05a90b-03b379f9 [goal] => 2014-10-25000 [amount] => 589.00 ) 
   [11] => Array ( [name] => R.Cervantes [id] => oper-4f05a90b-03b379f9 [goal] => 2014-10-25000 [amount] => 350.00 ) 
   [12] => Array ( [name] => A.Gerred [id] => oper-4f30019a-27f27473 [goal] => 2014-10-25000 [amount] => 375.00 ) 
   [13] => Array ( [name] => A.Gerred [id] => oper-4f30019a-27f27473 [goal] => 2014-10-25000 [amount] => 294.50 ) 
   [14] => Array ( [name] => A.Gerred [id] => oper-4f30019a-27f27473 [goal] => 2014-10-25000 [amount] => 440.00 ) 
   [15] => Array ( [name] => G.Whitcher [id] => oper-4f300d33-de9592e3 [goal] => 2014-10-25000 [amount] => 5.00 ) 
   [16] => Array ( [name] => G.Whitcher [id] => oper-4f300d33-de9592e3 [goal] => 2014-10-25000 [amount] => 310.00 ) 
   [17] => Array ( [name] => G.Whitcher [id] => oper-4f300d33-de9592e3 [goal] => 2014-10-25000 [amount] => 349.00 ) 
   [18] => Array ( [name] => K.Lawrence [id] => oper-50f6e4ad-9effbec7 [goal] => 2014-10-25000 [amount] => 499.00 ) 
   [19] => Array ( [name] => K.Lawrence [id] => oper-50f6e4ad-9effbec7 [goal] => 2014-10-25000 [amount] => 187.50 ) 
   [20] => Array ( [name] => K.Lawrence [id] => oper-50f6e4ad-9effbec7 [goal] => 2014-10-25000 [amount] => 170.00 ) 
   [21] => Array ( [name] => K.Chane [id] => oper-52657816-3d6516e2 [goal] => 2014-10-25000 [amount] => 375.00 ) 
   [22] => Array ( [name] => K.Chane [id] => oper-52657816-3d6516e2 [goal] => 2014-10-25000 [amount] => 187.50 ) 
   [23] => Array ( [name] => K.Chane [id] => oper-52657816-3d6516e2 [goal] => 2014-10-25000 [amount] => 229.50 ) 
   [24] => Array ( [name] => J.Stewart [id] => oper-qtgjvw8y-1uqtw058 [goal] => 2014-10-25000 [amount] => 170.00 ) 
   [25] => Array ( [name] => J.Stewart [id] => oper-qtgjvw8y-1uqtw058 [goal] => 2014-10-25000 [amount] => 584.00 ) 
   [26] => Array ( [name] => J.Stewart [id] => oper-qtgjvw8y-1uqtw058 [goal] => 2014-10-25000 [amount] => 249.50 ) )

到目前为止,这是我的代码(而且我知道它不干净,因为我还没有使用PDO,我只是想让它工作):

$result = mysql_query("SELECT a.*,u.OperatorName,u.MonthlyGoal
                      FROM tblUserPayments a
                        LEFT JOIN tblOperatorGoals u ON a.OperatorID = u.OperatorID
                      WHERE a.ChargeAmount IS NOT NULL 
                       AND a.PaymentStatus='OK' 
                       AND a.PaymentDate LIKE '$currentDate%' 
                       AND u.MonthlyGoal LIKE '$currentDate%' " );

while ($row = mysql_fetch_assoc($result)) {
  $operArray[] = array(
  'name' => $row['OperatorName'], 
  'id' => $row['OperatorID'],
  'goal' => $row['MonthlyGoal'], 
  'amount' => $row['ChargeAmount']);
}

foreach ($operArray as $value) {
  if($value['id'] == '' || $value['id'] == null) {
    continue;
  }
  if(array_key_exists($value['id'], $operSums)) {
    $operSums[$value['id']] += $value['amount'];
  } else {
    $operSums[$value['id']] = $value['amount'];
  }
}

我正在为每个运营商寻找一个如下所示的数组。非常感谢任何帮助。

Array ( [name] => BOB [id] => oper-%%%%%%%%%%%%% [goal] => 2014-10-25000 [amount] => $$total$$ )

1 个答案:

答案 0 :(得分:0)

您只需向SQL查询添加GROUP BY指令,并让数据库为您完成,而不是尝试在PHP中执行此操作。

E.g。 SELECT SUM(moneys) AS amount FROM mytable GROUP BY username将为同一个用户名创建一个包含多行的表,并将所有这些行折叠为一个。 “我们如何处理所有不同且丢失的数据?”使用“聚合器”功能可以解决问题。我们可以计算它们,或者获取最小值/最大值,尽管在你的情况下你想要使用sum,它(可预测地)只是在崩溃期间添加列moneys的所有值,然后将该数字作为新值名为amount的列。

现在,有了这个,请不要使用mysql_query

  1. 它不安全,你不应该使用它,更重要的是
  2. 它已经过时了,你不能使用它,因为现代PHP实际上不再带它了。只有过时的PHP版本才会升级,这个代码会立即被破坏。
  3. 请查看mysqli函数,它们是新的,安全的,支持的mysql连接器,或使用PDO但这通常很愚蠢,除非您知道您的脚本将与多种数据库格式接口(比如与mysql,postgres和sqlite文件交谈)