即使不在数组中,也要向表中添加行

时间:2010-08-30 08:23:56

标签: php arrays multidimensional-array

不确定如何标题,所以如果它具有误导性或不可理解,我会道歉。 我所拥有的是一个数组,该数组中将包含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 个答案:

答案 0 :(得分: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;
}