PHP merge array is simular
<p>How can I modify the below code so that it merges cell together that have the same data (in this case the same hex). At the moment it works with a max of 2 width.</p>

<pre><code>    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&lt;$spread;$i++){
            $newred=dechex($from[0]+round($i*$red));
            if(strlen($newred)&lt;2) $newred="0".$newred;

            $newgreen=dechex($from[1]+round($i*$green));
            if(strlen($newgreen)&lt;2) $newgreen="0".$newgreen;

            $newblue=dechex($from[2]+round($i*$blue));
            if(strlen($newblue)&lt;2) $newblue="0".$newblue;

            // Check if current hex is same as previous
            if($i &gt;= 1 &amp;&amp; (isset($return[$i-1]) &amp;&amp; $return[$i-1]['hex'] == "#".$newred.$newgreen.$newblue)){
                $return[$i-1]['width']++;
            } else {
                $return[$i] = array(
                        'hex' =&gt; "#".$newred.$newgreen.$newblue,
                        'width' =&gt; 1
                );
            }

        }

        return $return;
    }
</code></pre>

<p>Output:</p>

<pre><code>&lt;table cellspacing="0" cellpadding="0"&gt;
&lt;tr&gt;
    &lt;td bgcolor='#ffffff' width='2'&gt;&lt;/td&gt;
&lt;td bgcolor='#ffffff' width='1'&gt;&lt;/td&gt;
&lt;td bgcolor='#fefefe' width='2'&gt;&lt;/td&gt;
&lt;td bgcolor='#fefefe' width='2'&gt;&lt;/td&gt;
&lt;td bgcolor='#fdfdfd' width='2'&gt;&lt;/td&gt;
&lt;td bgcolor='#fdfdfd' width='2'&gt;&lt;/td&gt;
&lt;td bgcolor='#fcfcfc' width='2'&gt;&lt;/td&gt;
&lt;td bgcolor='#fcfcfc' width='2'&gt;&lt;/td&gt;
&lt;td bgcolor='#fbfbfb' width='2'&gt;&lt;/td&gt;
&lt;td bgcolor='#fbfbfb' width='2'&gt;&lt;/td&gt;
&lt;td bgcolor='#fafafa' width='2'&gt;&lt;/td&gt;
&lt;td bgcolor='#fafafa' width='2'&gt;&lt;/td&gt;
&lt;td bgcolor='#f9f9f9' width='2'&gt;&lt;/td&gt;
&lt;td bgcolor='#f9f9f9' width='1'&gt;&lt;/td&gt;
&lt;td bgcolor='#f8f8f8' width='1'&gt;&lt;/td&gt;
&lt;td bgcolor='#f6f6f6' width='1'&gt;&lt;/td&gt;
&lt;td bgcolor='#f3f3f3' width='1'&gt;&lt;/td&gt;
&lt;td bgcolor='#f1f1f1' width='1'&gt;&lt;/td&gt;
&lt;td bgcolor='#eeeeee' width='1'&gt;&lt;/td&gt;
&lt;td bgcolor='#ececec' width='1'&gt;&lt;/td&gt;
&lt;td bgcolor='#eaeaea' width='1'&gt;&lt;/td&gt;
&lt;td bgcolor='#e7e7e7' width='1'&gt;&lt;/td&gt;
&lt;td bgcolor='#e5e5e5' width='1'&gt;&lt;/td&gt;
&lt;td bgcolor='#e3e3e3' width='1'&gt;&lt;/td&gt;
&lt;td bgcolor='#e0e0e0' width='1'&gt;&lt;/td&gt;
&lt;td bgcolor='#dedede' width='1'&gt;&lt;/td&gt;
&lt;td bgcolor='#dbdbdb' width='1'&gt;&lt;/td&gt;
&lt;td bgcolor='#d9d9d9' width='1'&gt;&lt;/td&gt;
</code></pre>
