Function not going through the 2nd part of the array

My eyes keep going square trying to get through the code, but I think this might be a problem. At the start of the function, you open this loop:

for($a=0; $a<count($keyword_colorArr);$a++){

which iterates through that array. However, on line 145 of your code, still within that main loop as far as I can see, you open this second loop:

for($a=0; $a<count($keyword_colorArr);$a++){

It looks to me like that will end with the value of $a equalling count($keyword_colorArr) the first time you run through the loop, so your outer loop exits without running the second (or any subsequent) pass.