-1
如何修改下面的代碼,以便它將具有相同數據(在本例中爲相同的十六進制)的單元合併在一起。目前它的最大工作寬度爲2。PHP合併數組是相似的
function generateArray($from, $until, $spread){
$from = array(hexdec(substr($from,0,2)),hexdec(substr($from,2,2)),hexdec(substr($from,4,2)));
$until = array(hexdec(substr($until,0,2)),hexdec(substr($until,2,2)),hexdec(substr($until,4,2)));
$red=($until[0]-$from[0])/($spread-1);
$green=($until[1]-$from[1])/($spread-1);
$blue=($until[2]-$from[2])/($spread-1);
for($i=0;$i<$spread;$i++){
$newred=dechex($from[0]+round($i*$red));
if(strlen($newred)<2) $newred="0".$newred;
$newgreen=dechex($from[1]+round($i*$green));
if(strlen($newgreen)<2) $newgreen="0".$newgreen;
$newblue=dechex($from[2]+round($i*$blue));
if(strlen($newblue)<2) $newblue="0".$newblue;
// Check if current hex is same as previous
if($i >= 1 && (isset($return[$i-1]) && $return[$i-1]['hex'] == "#".$newred.$newgreen.$newblue)){
$return[$i-1]['width']++;
} else {
$return[$i] = array(
'hex' => "#".$newred.$newgreen.$newblue,
'width' => 1
);
}
}
return $return;
}
輸出:
<table cellspacing="0" cellpadding="0">
<tr>
<td bgcolor='#ffffff' width='2'></td>
<td bgcolor='#ffffff' width='1'></td>
<td bgcolor='#fefefe' width='2'></td>
<td bgcolor='#fefefe' width='2'></td>
<td bgcolor='#fdfdfd' width='2'></td>
<td bgcolor='#fdfdfd' width='2'></td>
<td bgcolor='#fcfcfc' width='2'></td>
<td bgcolor='#fcfcfc' width='2'></td>
<td bgcolor='#fbfbfb' width='2'></td>
<td bgcolor='#fbfbfb' width='2'></td>
<td bgcolor='#fafafa' width='2'></td>
<td bgcolor='#fafafa' width='2'></td>
<td bgcolor='#f9f9f9' width='2'></td>
<td bgcolor='#f9f9f9' width='1'></td>
<td bgcolor='#f8f8f8' width='1'></td>
<td bgcolor='#f6f6f6' width='1'></td>
<td bgcolor='#f3f3f3' width='1'></td>
<td bgcolor='#f1f1f1' width='1'></td>
<td bgcolor='#eeeeee' width='1'></td>
<td bgcolor='#ececec' width='1'></td>
<td bgcolor='#eaeaea' width='1'></td>
<td bgcolor='#e7e7e7' width='1'></td>
<td bgcolor='#e5e5e5' width='1'></td>
<td bgcolor='#e3e3e3' width='1'></td>
<td bgcolor='#e0e0e0' width='1'></td>
<td bgcolor='#dedede' width='1'></td>
<td bgcolor='#dbdbdb' width='1'></td>
<td bgcolor='#d9d9d9' width='1'></td>