2017-07-26 133 views
1

我有這樣的設置,安裝托架顏色fusionchart條形圖產生12個隨機顏色每月

"paletteColors": "#594a2d,#660057,#6e8cff,#3d0c4e,#9ce6ae,#009e78,#001f8f,#ff7852,#ebff26,#0073e6,#f05eff,#00decc", 

我嘗試生成全年的每個月12個隨機顏色代碼放在設置..

或者使用上面的12種顏色代碼,但是對於每個循環來說都是隨機的?

#594a2d,#660057,#6e8cff,#3d0c4e,#9ce6ae,#009e78,#001f8f,#ff7852,#ebff26,#0073e6,#f05eff,#00decc 

我到目前爲止嘗試..這是工作隨機顏色..但如果我想使用預定義的顏色?

<?php $rand = dechex(rand(0x000000, 0xFFFFFF));?> 

"paletteColors": "<?php echo('#' . $rand); 

for ($d=0;$d<12;$d++) 
{ 
echo(',#' . $rand); 
} 

?>", 

回答

3

shuffle函數將陣列的元素移動到隨機位置。因此,你可以洗牌與預定義的顏色組成的數組,並使用它的前十二個要素:

$colors = explode(',', '#594a2d,#660057,#6e8cff,#3d0c4e,#9ce6ae,#009e78,#001f8f,#ff7852,#ebff26,#0073e6,#f05eff,#00decc'); 

shuffle($colors); 
for ($i = 0; $i < 12; $i++) { 
    echo($colors[$i]); 
} 
+0

非常感謝你這是它 – gtroop

3

使用功能洗牌()

$arr = [1,2,3,4,5,6,7,8,9,10,11,12]; 
shuffle($arr); 
print_r($arr); 

結果是

Array ([0] => 8 [1] => 2 [2] => 6 [3] => 10 [4] => 1 [5] => 9 [6] => 3 [7] => 5 [8] => 4 [9] => 12 [10] => 7 [11] => 11)