Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6819079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:15:47+00:00 2026-05-26T21:15:47+00:00

I have an array of hex color codes and want to loop through them

  • 0

I have an array of hex color codes and want to loop through them and compare to a database entry. Here is the code:

// database connect & query hidden for brevity

$row = mysql_fetch_assoc($result);

$colors =  array("EEEEEE","EEEED8","EEEEAA","EEEE9A","EEEE77","EEEE49","EEEE21","EEEE00",
                 "DAFFFF","DAFFF3","DAFFDA","DAFFD2","DAFFBF","DAFFA6","DAFF91","DAFF7F",
                 "9FFFFF","9FFFED","9FFFC8","9FFFBB","9FFF9F","9FFF7A","9FFF5A","9FFF3F",
                 "19FFFF","19FFEA","19FFBD","19FFAE","19FF8C","19FF60","19FF39","19FF19",
                 "BBC8FF","9FC8FF","7AC8FF","5AC8FF","3FC8FF","7A7AFF","5A7AFF","3F7AFF",
                 "BB7AFF","9F7AFF","608CFF","398CFF","198CFF","6039FF","3939FF","1939FF",
                 "EEAAAA","EEAAEE","EEAAD8","FF91D2","C87AFF","8C39FF","BD39FF","AE39FF",
                 "EEAA77","EEAA49","EEAA21","EEAA00","FF5A3F","FF6039","FF6019","FF3919",
                 "FF5A9F","FF5A7A","FF5A5A","FF6060","FF3960","FF3F5A","FF3F3F","FF3939");


if(!in_array($row['color1'],$colors))
{
    $colors[] = $row['color1'];
}
if(!in_array($row['color2'],$colors))
{
    $colors[] = $row['color2'];
}

$count = count($colors);

echo "The Database says Color1 is: ".$row['color1']."<br>";
echo "The Colors Array says it is: ".$colors[73]."<br><br>";
echo "The Database says Color2 is: ".$row['color2']."<br>";
echo "The Colors Array says it is: ".$colors[74]."<br><br><br>";

echo "Going Through the Loops shows:<br><br>";

for($i=0;$i<$count;$i++)
{
    if(('#'.$colors[$i]) == $row['color1'])
        echo 'FOUND IT!!! ----&gt; '.$colors[$i].'<br>';
    else
        echo 'Nope, not this one... '.$colors[$i].'<br>';
}

echo "&lt;br&gt;And Printing the Array we see:<br><br><br>";
print_r($colors);

The problem is the second to last iteration through the loop prints a null and the last color is lost entirely. I’ve tried this several different ways and all result in the same.

Here is the output:

The Database says Color1 is: 51A800
The Colors Array says it is: 51A800

The Database says Color2 is: 8FC400
The Colors Array says it is: 8FC400

Going Through the Loops shows:

Nope, not this one... EEEEEE
Nope, not this one... EEEED8
Nope, not this one... EEEEAA
Nope, not this one... EEEE9A
Nope, not this one... EEEE77
Nope, not this one... EEEE49
Nope, not this one... EEEE21
Nope, not this one... EEEE00
Nope, not this one... DAFFFF
Nope, not this one... DAFFF3
Nope, not this one... DAFFDA
Nope, not this one... DAFFD2
Nope, not this one... DAFFBF
Nope, not this one... DAFFA6
Nope, not this one... DAFF91
Nope, not this one... DAFF7F
Nope, not this one... 9FFFFF
Nope, not this one... 9FFFED
Nope, not this one... 9FFFC8
Nope, not this one... 9FFFBB
Nope, not this one... 9FFF9F
Nope, not this one... 9FFF7A
Nope, not this one... 9FFF5A
Nope, not this one... 9FFF3F
Nope, not this one... 19FFFF
Nope, not this one... 19FFEA
Nope, not this one... 19FFBD
Nope, not this one... 19FFAE
Nope, not this one... 19FF8C
Nope, not this one... 19FF60
Nope, not this one... 19FF39
Nope, not this one... 19FF19
Nope, not this one... BBC8FF
Nope, not this one... 9FC8FF
Nope, not this one... 7AC8FF
Nope, not this one... 5AC8FF
Nope, not this one... 3FC8FF
Nope, not this one... 7A7AFF
Nope, not this one... 5A7AFF
Nope, not this one... 3F7AFF
Nope, not this one... BB7AFF
Nope, not this one... 9F7AFF
Nope, not this one... 608CFF
Nope, not this one... 398CFF
Nope, not this one... 198CFF
Nope, not this one... 6039FF
Nope, not this one... 3939FF
Nope, not this one... 1939FF
Nope, not this one... EEAAAA
Nope, not this one... EEAAEE
Nope, not this one... EEAAD8
Nope, not this one... FF91D2
Nope, not this one... C87AFF
Nope, not this one... 8C39FF
Nope, not this one... BD39FF
Nope, not this one... AE39FF
Nope, not this one... EEAA77
Nope, not this one... EEAA49
Nope, not this one... EEAA21
Nope, not this one... EEAA00
Nope, not this one... FF5A3F
Nope, not this one... FF6039
Nope, not this one... FF6019
Nope, not this one... FF3919
Nope, not this one... FF5A9F
Nope, not this one... FF5A7A
Nope, not this one... FF5A5A
Nope, not this one... FF6060
Nope, not this one... FF3960
Nope, not this one... FF3F5A
Nope, not this one... FF3F3F
Nope, not this one... FF3939
Nope, not this one... 
Nope, not this one... 51A800

And Printing the Array we see:

Array ( [0] => EEEEEE [1] => EEEED8 [2] => EEEEAA [3] => EEEE9A [4] => EEEE77 [5] => EEEE49 [6] => EEEE21 [7] => EEEE00 [8] => DAFFFF [9] => DAFFF3 [10] => DAFFDA [11] => DAFFD2 [12] => DAFFBF [13] => DAFFA6 [14] => DAFF91 [15] => DAFF7F [16] => 9FFFFF [17] => 9FFFED [18] => 9FFFC8 [19] => 9FFFBB [20] => 9FFF9F [21] => 9FFF7A [22] => 9FFF5A [23] => 9FFF3F [24] => 19FFFF [25] => 19FFEA [26] => 19FFBD [27] => 19FFAE [28] => 19FF8C [29] => 19FF60 [30] => 19FF39 [31] => 19FF19 [32] => BBC8FF [33] => 9FC8FF [34] => 7AC8FF [35] => 5AC8FF [36] => 3FC8FF [37] => 7A7AFF [38] => 5A7AFF [39] => 3F7AFF [40] => BB7AFF [41] => 9F7AFF [42] => 608CFF [43] => 398CFF [44] => 198CFF [45] => 6039FF [46] => 3939FF [47] => 1939FF [48] => EEAAAA [49] => EEAAEE [50] => EEAAD8 [51] => FF91D2 [52] => C87AFF [53] => 8C39FF [54] => BD39FF [55] => AE39FF [56] => EEAA77 [57] => EEAA49 [58] => EEAA21 [59] => EEAA00 [60] => FF5A3F [61] => FF6039 [62] => FF6019 [63] => FF3919 [64] => FF5A9F [65] => FF5A7A [66] => FF5A5A [67] => FF6060 [68] => FF3960 [69] => FF3F5A [70] => FF3F3F [71] => FF3939 [73] => 51A800 [74] => 8FC400 ) 

I’ve never had a problem with a simple array before… What am I not seeing? I have a terrible feeling this is one of those stupid things where it’s a stupid zero-width space or something stupid like that, but I can’t find it…

I appreciate the help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:15:47+00:00Added an answer on May 26, 2026 at 9:15 pm

    This code:

    if(!in_array($row['color1'],$colors))
    {
        $colors[] = $row['color1'];
    }
    if(!in_array($row['color2'],$colors))
    {
        $colors[] = $row['color2'];
    }
    

    Will insert a blank entry into the colors array once if either $row['color1'] or $row['color2'] is blank / empty. Modify the check to something like this, which should get rid of your empty value.

    if( !empty( $row['color1']) && !in_array($row['color1'],$colors))
    {
        $colors[] = $row['color1'];
    }
    if( !empty( $row['color2']) &&  !in_array($row['color2'],$colors))
    {
        $colors[] = $row['color2'];
    }
    

    Edit: Here is one way to replicate your loop output.

    1. You have your indexes mixed up, 74 does not exist.
    2. $row['color1'] is empty, $row['color2'] is 51A800
    3. You probably have warnings / error reporting turned off

    See the demo below, it’s exactly replicating the output from the loop.

    Demo

    EDIT: Found the blank entry

    I was wrong to think the entry is blank – It’s actually not present in the array. Check out the array output above:

    [71] => FF3939 [73] => 51A800 [74] => 8FC400 )
                  ^^
                 ????
    

    If you loop based on count( $array), you get 73, which is the correct number of elements. You loop from 0-73, while 72 does NOT exist. You never get 74 because there are only 73 elements in the array, so you never get to index 74. Figure out why index 72 is missing / gone, and you’ll solve entire problem. Perhaps the real problem lies in code that was not pasted to SO.

    Running with this, here is a demo that replicates the output (if error reporting was turned off, it would), if somewhere along the line, 72 gets erased.

    Finally, here is a demo that replicates the output exactly if some entry is inserted into the array and then removed before the $row colors are inserted into the $colors array. My best guess is that this is what is happening in some part of the OP’s omitted code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an unsigned char array and I want to convert it to hex
VISUAL C++ Question Hi, I have array of 3 elements and I want to
Hi i am making application in c#.I have byte array of containing hex values.I
i have a very long hex string in a byte array and I would
I have an array of hex numbers, and I need to go over other
I have to convert a normal string array with the value {STX,ETX} into hex
I have test array of hex(radix 16) numbers var numbers = [01,02,a1]; and I
I have a hex NSString and I want to be able to pack that
i have a 1D array. I copied the data onto a 8 bit color
I have an array full of NSString's in hex and what to create a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.