Php 将行添加到表中,即使不在数组中
不知道该如何命名,所以如果有误导或不可理解的地方,我道歉。 我拥有的是一个数组,该数组将包含1-4个数组。我需要一个HTML表来输出4列,所以即使数组中只有一个数组,它仍然需要输出列。我提到的问题是,专栏需要匹配。在数组内部有一个名为“leverable”的键,该键与HTML表的列匹配。数组、PHP、当前HTML输出和所需的HTML输出都在下面,如果这篇文章没有正确显示它们,将提供一个指向PasteBin的链接Php 将行添加到表中,即使不在数组中,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,不知道该如何命名,所以如果有误导或不可理解的地方,我道歉。 我拥有的是一个数组,该数组将包含1-4个数组。我需要一个HTML表来输出4列,所以即使数组中只有一个数组,它仍然需要输出列。我提到的问题是,专栏需要匹配。在数组内部有一个名为“leverable”的键,该键与HTML表的列匹配。数组、PHP、当前HTML输出和所需的HTML输出都在下面,如果这篇文章没有正确显示它们,将提供一个指向PasteBin的链接 // OUTPUT OF $estimateOutput array ( 0
// 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>
//$estimateOutput的输出
排列(
0 =>
排列(
“AdminCode”=>“NASC”,
“AdminName”=>
排列(
),
“NewUsed”=>“Used”,
“Coverage Code”=>“NCGUGOLD”,
'CoverageName'=>'GOLD USED COMPONENT 1-10',
“一般覆盖范围”=>
排列(
),
“期限”=>“24/24”,
'终止月份'=>'24',
'TermInMilesKM'=>'24000',
“免赔额”=>“100”,
“零售成本”=>“1377.0”,
),
1 =>
排列(
“AdminCode”=>“NASC”,
“AdminName”=>
排列(
),
“NewUsed”=>“Used”,
“Coverage Code”=>“NCGUGOLD”,
'CoverageName'=>'GOLD USED COMPONENT 1-10',
“一般覆盖范围”=>
排列(
),
“期限”=>“24/24”,
'终止月份'=>'24',
'TermInMilesKM'=>'24000',
“免赔额”=>“50”,
“零售成本”=>“1462.0”,
),
)
$estimateOutput.='。$term_length.'Months';
如果($rateEstimate!=“”){
foreach($rate估算为$rate){
$ratesByMiles[$rate[“TermInMilesKM”][]=$rate;
}
foreach($rateEstimate作为$key=>$value){
如果($TERMINILESKM!=$value['TERMINILESKM'])){
$estimateOutput.='';
$estimateOutput.=''.number_格式($value['terminileskm'])。';
foreach($ratesByMiles[$value['TermInMilesKM']]as$newval){
$estimateOutput.='';
$estimateOutput.='';
$estimateOutput.='';
}
$estimateOutput.='';
$TermInMilesKM=$value['TermInMilesKM'];
}
}
}否则{
$estimateOutput.='不可用';
}
echo$估计输出;
//当前输出
24个月
24,000
//需要它输出吗
24个月
24,000
--//如果$newval['Delible']==0,则应显示数据,否则--
//如果$newval['Equired']==50,则应显示数据,否则--
//如果$newval['Equired']==100,则应显示数据,否则--
--//如果$newval['Durelible']==200,则应显示数据,否则--
始终循环四次,然后找到要打印的正确数组:
$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;
}
通过一些调整,这正是我想要的,非常感谢!只浪费了5个小时绕圈子跑。。。