2010-08-30 45 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

相关问题