2010-08-30 44 views
0

不知道如何爲此標題,因此如果它具有誤導性或無法理解,我表示歉意。 我所擁有的是一個數組,該數組中將有1-4個數組。我需要一個HTML表格來輸出4列,所以即使數組只有一個數組,它仍然需要輸出列。我遇到的問題是列需要匹配。數組內部有一個名爲'Deductible'的鍵,該鍵與HTML表格的列匹配。數組,PHP,當前HTML輸出和想要的HTML輸出都在下面,並且指向PasteBin的鏈接,以防這篇文章無法正確顯示。即使不在數組中,也會將行添加到表中

// OUTPUT OF $estimateOutput 

array (
    0 => 
    array (
    'AdminCode' => 'NASC', 
    'AdminName' => 
    array (
    ), 
    'NewUsed' => 'Used', 
    'CoverageCode' => 'NCGUGOLD', 
    'CoverageName' => 'GOLD USED COMPONENT 1-10', 
    'GenericCoverage' => 
    array (
    ), 
    'Term' => '24/24', 
    'TermInMonths' => '24', 
    'TermInMilesKM' => '24000', 
    'Deductible' => '100', 
    'RetailCost' => '1377.0', 
), 
    1 => 
    array (
    'AdminCode' => 'NASC', 
    'AdminName' => 
    array (
    ), 
    'NewUsed' => 'Used', 
    'CoverageCode' => 'NCGUGOLD', 
    'CoverageName' => 'GOLD USED COMPONENT 1-10', 
    'GenericCoverage' => 
    array (
    ), 
    'Term' => '24/24', 
    'TermInMonths' => '24', 
    'TermInMilesKM' => '24000', 
    'Deductible' => '50', 
    'RetailCost' => '1462.0', 
), 
) 


$estimateOutput .= '<tr class="month-section"><td colspan="5"><strong>'.$term_length.' Months</strong></td></tr>'; 
if($rateEstimate != ""){ 
    foreach($rateEstimate as $rate) { 
    $ratesByMiles[$rate["TermInMilesKM"]][] = $rate; 
    } 
    foreach($rateEstimate as $key=>$value){ 
    if($TermInMilesKM != $value['TermInMilesKM']){ 
     $estimateOutput .= '<tr>'; 
     $estimateOutput .= '<td>'.number_format($value['TermInMilesKM']).'</td>'; 

     foreach($ratesByMiles[$value['TermInMilesKM']] as $newval){ 
     $estimateOutput .= '<td>'; 
     $estimateOutput .= '<a href="#" rel="'.$newval['CoverageCode'].'::'.$newval['Term'].'::'.$newval['Deductible'].'::'.$newval['RetailCost'].'">$'; 
     $estimateOutput .= number_format($newval['RetailCost']); 
     $estimateOutput .= '</a>'; 
     $estimateOutput .= '</td>'; 
     } 
     $estimateOutput .= '</tr>'; 
     $TermInMilesKM = $value['TermInMilesKM']; 
    } 
    } 
} else { 
    $estimateOutput .= '<tr><td colspan="5">Not Available</td></tr>'; 
} 
echo $estimateOutput; 



// CURRENTLY OUTPUTTING 
<tr class="month-section" style="display: table-row;"> 
    <td colspan="5"><strong>24 Months</strong></td> 
</tr> 
<tr style="display: table-row;"> 
    <td>24,000</td> 
    <td><a rel="NCGUGOLD::24/24::50::1462.0" href="#">$1,462</a></td> 
    <td><a rel="NCGUGOLD::24/24::100::1377.0" href="#">$1,377</a></td> 
</tr> 



// NEED IT TO OUTPUT 

<tr class="month-section" style="display: table-row;"> 
    <td colspan="5"><strong>24 Months</strong></td> 
</tr> 
<tr style="display: table-row;"> 
    <td>24,000</td> 
    <td>--</td> // If $newval['Deductible'] == 0 this should show data, otherwise -- 
    <td><a rel="NCGUGOLD::24/24::50::1462.0" href="#">$1,462</a></td> // If $newval['Deductible'] == 50 this should show data, otherwise -- 
    <td><a rel="NCGUGOLD::24/24::100::1377.0" href="#">$1,377</a></td> // If $newval['Deductible'] == 100 this should show data, otherwise -- 
    <td>--</td> // If $newval['Deductible'] == 200 this should show data, otherwise -- 
</tr> 

http://pastebin.com/y41MA8VZ

回答

1

始終循環四次,然後找到正確的陣列打印:

$deductable_values = array(0, 50, 100, 200); 
foreach ($deductable_values as $deductable_value) { 
    $data = find_deductible_array($estimateOutput, $deductable_value); 
    // output a row using $data, which may be null. 
} 

function find_deductible_array($estimateOutput, $value) { 
    foreach ($estimateOutput as $row) { 
     if ($row['deductible'] == $value) { 
      return $row; 
     } 
    } 
    // not found 
    return null; 
} 
+0

有了一些調整這也正是我一直在尋找的,非常感謝你!在圈子裏浪費了5個小時...... – LostInQuery 2010-08-30 08:46:47

相關問題