纠正和修复爆炸并排序以在PHP中显示结果

I just create a color extractor for my gallery. for every image i have a row in database for colors, this is a part of one of those row in database.

Example of row:

CCCCCC,CCCCFF,99CCCC,333333,9999CC,999999,CCFFFF,000000,000033,669999,666699,333366,003333,336666,666666,FFFFFF,FF9900,333300...

i didn't add all colors in here because is too long.

Now, in photo page i need to show those colors, but not all of them. for example just 20 color. so i wrote this code:

$all_colors = $db->super_query("SELECT color 
FROM colors 
WHERE photo_id='{$photoid}'
");

$list = explode(',',$all_colors['color'], 20);//Select 20 colors
sort($list); //And sorting colors

foreach ($list as $item){
echo $item."<br>";
}

Now the problem is, this is the result from that code:

000000
000033
003333
330000
333300
333333
333366
336666
663300
666633
666666
666699
669999
996633,FFFFCC,CC9966,CCCC99,663333,FF9933,FFCC66,CC9933,CC6600,666600,996600,003300,999966,FFCC33,CCFFCC,996666,999933,FFCC00,CC9999,FFFF99,FF9999
999999
9999CC
99CCCC
CCCCCC
CCCCFF
CCFFFF

You can see a line with too many colors!

Result must be like this:

000000
000033
003333
330000
333300
333333
333366
336666
663300
666633
666666
666699
669999
996633
999999
9999CC
99CCCC
CCCCCC
CCCCFF
CCFFFF

How i can fix this code?

Option #1

$all_colors = $db->super_query("SELECT color 
FROM colors 
WHERE photo_id='{$photoid}'
ORDER BY color
LIMIT 20");

$list = explode(',',$all_colors['color']);

foreach ($list as $item){
    echo $item."<br>";
}

Option #2

$all_colors = $db->super_query("SELECT color 
FROM colors 
WHERE photo_id='{$photoid}'");

$list = array_slice(explode(',',$all_colors['color']), 0, 20);//Select 20 colors
sort($list); //And sorting colors

foreach ($list as $item){
    echo $item."<br>";
}

Option #3

$all_colors = $db->super_query("SELECT color 
FROM colors 
WHERE photo_id='{$photoid}'");

$list = explode(',',$all_colors['color'], 21);//Select 20 colors
if (count($list) == 21)
    array_pop($list);
sort($list); //And sorting colors

foreach ($list as $item){
    echo $item."<br>";
}

Use array_slice() to extract the first 20 elements of the array.

$list = array_slice(explode(',',$all_colors['color'], 21), 0, 20);
print_r($list);

Result:

Array
(
    [0] => CCCCCC
    [1] => CCCCFF
    [2] => 99CCCC
    [3] => 333333
    [4] => 9999CC
    [5] => 999999
    [6] => CCFFFF
    [7] => 000000
    [8] => 000033
    [9] => 669999
    [10] => 666699
    [11] => 333366
    [12] => 003333
    [13] => 336666
    [14] => 666666
    [15] => FFFFFF
    [16] => FF9900
    [17] => 333333
    [18] => 333366
    [19] => 336666
)

Try this:

$all_colors = $db->super_query("SELECT color 
FROM colors 
WHERE photo_id='{$photoid}'
");

//Select all colors, third parameter limits, but last one will be remaining string
$list = explode(',',$all_colors['color']);
sort($list); //And sorting colors

$c = 0;
foreach ($list as $item){
echo $item."<br>";
//if c equals 20 break
if($c==20)
break;
$c++;
}

Noe: This is quick suggestion, method that works. I am sure there are better ways doing same thing.