2016-09-23 87 views
-3
<?php 
error_reporting(E_ALL); 
$test_array = Array(Array 
(
    "pid" => 1, 
    "encounter" => 20, 
    "code" => abc, 
    "fee" => 300.00 
), 
Array 
(
    "pid" => 1, 
    "encounter" => 20, 
    "code" => abc, 
    "fee" => 300.00 
), 
Array 
(
    "pid" => 2, 
    "encounter" => 20, 
    "code" => abc, 
    "fee" => 80 
), 
Array 
(
    "pid" => 3, 
    "encounter" => 20, 
    "code" => xyz, 
    "fee" => 90 
), 
Array 
(
    "pid" => 5, 
    "encounter" => 40, 
    "code" => xyz, 
    "fee" => 100 
), 
Array 
(
    "pid" => 3, 
    "encounter" => 40, 
    "code" => xyz, 
    "fee" => 100 
), 
Array 
(
    "pid" => 2, 
    "encounter" => 20, 
    "code" => abc, 
    "fee" => 80 
), 
Array 
(
    "pid" => 1, 
    "encounter" => 20, 
    "code" => xyz, 
    "fee" => 40 
)); 
//Declaration... 
$pre_pid = ""; 
$pre_encounter = ""; 
$pre_code = ""; 
$pre_fee = ""; 
$sum_charges = 0; 
/*Foreach loop*/ 
$i=0; 
foreach($test_array as $my_arr){ 
    $pre_pid = $my_arr['pid']; 
    $pre_encounter = $my_arr['encounter']; 
    $pre_code = $my_arr['code']; 
    if($pre_pid == $my_arr['pid'] && $pre_encounter == $my_arr['encounter'] && $pre_code == $my_arr['code']){ 
     echo "FEE-AMOUNT=".$my_arr['fee']; 
     $sum_charges+=$my_arr['fee']; 
     echo '<br/>'; 
    } 
$i++; 
} 
//Getting Sum = 1090 
//Actual Sum I needed = 710 
?> 

你好朋友我想上面的代碼,我想要費用應該計算誰擁有相同的3鍵值對。 例如,如果每個數組3鍵值相同,則只計算這些費用數量。PHP陣列總和通過相同的鍵值對需要計算

+1

這裏實施代碼,不使用引擎收錄或別的東西,這些鏈接arn't永久.. – Naruto

+0

粘貼您的代碼在這裏 –

+0

或陣列例如你試試這個[答案]之前和之後 –

回答

0

如果我理解你的問題,你需要總結的費列每次3個其他列具有相同的值。我定製了一下你的代碼,這樣你就可以爲你的每個元素提供總成本。

//Declaration... 
$pre_pid = ""; 
$pre_encounter = ""; 
$pre_code = ""; 
$pre_fee = ""; 
$sum_charges = 0; 
$sum_array = array(); 
$total_fees = 0; 
/*Foreach loop*/ 

foreach($test_array as &$my_arr){ 

    $pre_pid = $my_arr['pid']; 
    $pre_encounter = $my_arr['encounter']; 
    $pre_code = $my_arr['code']; 
    $pre_fee = $my_arr['fee']; 
    $fee_ammount = $pre_fee; 
    $duplicates_check = array(); 

    foreach($test_array as $value) { 
     if($pre_pid == $value['pid'] && ($pre_encounter != $value['encounter'] || $pre_code != $value['code'])){ 

      $duplicate = false; 

      foreach($duplicates_check as $duplicate_array) { 
       if($duplicate_array == $value) 
        $duplicate =true; 
      } 
      if(!$duplicate) { 
       $fee_ammount += $value['fee']; 
       $duplicates_check[] = $value; 
      } 
     } 
    } 
    $my_arr['total_fee'] = $fee_ammount; 

    if(!isset($sum_array[$my_arr['pid']])) { 
     $sum_array[$my_arr['pid']] = $my_arr['total_fee']; 
     echo 'pid => '.$my_arr['pid'].', total fees => '.$my_arr['total_fee'].'<br />'; 
     $total_fees += $my_arr['total_fee']; 
    } 
} 

echo 'Total : '.$total_fees; 
var_dump($sum_array); 
+0

不工作先生我的預期ANS是710 –

+0

我人工檢查,不存在用於710總和匹配pid,遇到和代碼。 你能告訴我們需要添加哪些數組來獲得710作爲答案嗎? – Alex

+0

的Pid 1(僅300 + 40)的Pid 2(80)的Pid 3(90 + 100)的pid 5(100)在此PID = 1的情況下,我想300僅一次,因爲相同的PID,遭遇和代碼。 pid = 3個不同的遭遇,這就是爲什麼100加入。 –