2012-11-22 36 views
0

即時顯示具有2個數據數組的顯示。第一個數組包含銷售經理的數據,第二個數組包含每個總部的總價值。訪問數組中的單個值並根據條件

下面的代碼顯示了銷售經理的細節,它工作正常。

<?php foreach ($allsales as $sales):?> 
<?php if ($z != $sales['class']): ?> 

    <tr> 
    <td colspan = "6"><?php echo $sales['scode'];?><hr /> </td> 
    </tr> 

<?php endif; ?> 
<tr> 
    <td colspan = "2"><?php echo $sales['numb']?></td> 
    <td><?php echo $sales['name']?></td> 
    <td><?php echo $sales['scode']?></td> 
    <td style="text-align: center"><?php echo $sales['months']?> Months</td> 


    <?php 
    $pending_unf = $sales['amount'] * $sales['months']; 
    $pending = number_format((float)$pending_unf, 2, '.', ''); 
    ?> 
    <td style="text-align: right"><?php echo $pending;?></td> 

    </tr> 
<tr> 
    <td colspan = "6">Total Amount : XXXX</td> 
    </tr> 

    <?php $z = $sales['scode']; ?> 
    <?php endforeach;?> 

下面是我的第二個數組數據

array(11) { 
    [0]=> 
    array(2) { 
    ["scode"]=> 
    string(3) "10A" 
    ["amount"]=> 
    string(5) "12600" 
    } 
    [1]=> 
    array(2) { 
    ["scode"]=> 
    string(3) "10B" 
    ["amount"]=> 
    string(5) "51600" 
    } 
    [2]=> 
    array(2) { 
    ["scode"]=> 
    string(3) "11A" 
    ["amount"]=> 
    string(5) "60000" 
    } 
    [3]=> 
    array(2) { 
    ["scode"]=> 
    string(3) "9B" 
    ["amount"]=> 
    string(5) "30000" 
    } 

我想什麼做的是從第二陣列獲得的金額,並在每個SCODE的端顯示,在總作爲新行組如下

admission name months scode 
================================== 
001  test1 3  10A 
002  test2 5  10A 
       Total USD 1000 

006  test3 7  15B 
       Total USD 1800 

008  test4 1  15A 
       Total USD 800 

011  test5 2  16C 
       Total USD 1600 

051  test6 3  16A 
012  test7 3  16A 
       Total USD 2700 

我只是試圖使用一個foreach,但它不斷重複在每一行。但我希望它僅在每個組的結尾處顯示。

那麼我如何訪問第二個數組中的單個值,並在條件語句中使用它來顯示每個組中最後一個scode的末尾?

任何幫助將不勝感激。

回答

0

我會做的是使用兩個循環和一個「已讀」scodes數組。 現在,在第一個循環中,每當我碰巧在第一個數組上找到新的scode時,我會將它放在「讀取scode」數組中,然後切換到內部循環,該循環從當前位置遍歷所有數組,並讀取具有相同評分的用戶。 像這樣(假設你有函數來尋找secon數組中的「總值」)

<?php 
    $array1 = /*array of users*/; 
    $array2 = /*array of total amouns of whatever*/; 
    $read = array(); //theese scodes should be skipped next time 
    for($i=0; $i<count($array1); $i++) { 
    if(in_array($array1[$i]['scode'],$read)) 
     continue; //If this user was already outputed, we skip him 
       //Othewise, we output all users with same SCODE 
    $read[] = $array1[$i]['scode']; 
    for($j=$i; $j<count($array1); $j++) { //Note that I start from current position, all inputs before must be already read 
     echo "User: {$array1[$j]['name']}\nScode: {$array1[$j]['scode']}\n\n"; 
    } 
    echo "Total amount for theese users: ".get_total_amount($array1[$i]['scode'],$array2).PHP_EOL.PHP_EOL.PHP_EOL; //finding amount for current scode. 
    } 

?> 
相關問題