2012-04-25 50 views
2

我將單一顏色推入一個數組,以便用單一顏色覆蓋每個條形圖的顏色,但其不能按預期工作,數組有問題。如何糾正一個foreach循環並獲得想要的數組深度?

我原來的問題:如何將數組推到barChart和顏色覆蓋pChart中的單色?

已更新問題: print_r結果顯示額外的2組數組,它​​應該只是一個數組,它不會更深,它們在哪裏被添加?我如何糾正它?

PHP/pChart:

$prop_open=(1,5,8,4,2,66); 

$j=0; 
$palette_cycle=array(); 
foreach($prop_open as $value) { 
    array_push($palette_cycle,array("$j"=>array("R"=>108,"G"=>157,"B"=>49,"Alpha"=>100))); 
    $j++; 
} 

$palette=$palette_cycle; 
$myPicture->drawBarChart(array("OverrideColors"=>$palette,"DisplayOrientation"=>ORIENTATION_HORIZONTAL,"DisplayPos"=>LABEL_POS_INSIDE,"DisplayValues"=>TRUE,"DisplayColor"=>DISPLAY_MANUAL,"DisplayR"=>0,"DisplayG"=>0,"DisplayB"=>0,"Surrounding"=>-60,"InnerSurrounding"=>60)); 

這是我的$ palette_cycle數組應該如何結束後對其做期待(但所有RGB是相同的):

$palette_cycle = array("0"=>array("R"=>188,"G"=>224,"B"=>46,"Alpha"=>100), 
       "1"=>array("R"=>224,"G"=>100,"B"=>46,"Alpha"=>100), 
       "2"=>array("R"=>224,"G"=>214,"B"=>46,"Alpha"=>100), 
       "3"=>array("R"=>46,"G"=>151,"B"=>224,"Alpha"=>100), 
       "4"=>array("R"=>176,"G"=>46,"B"=>224,"Alpha"=>100), 
       "5"=>array("R"=>224,"G"=>46,"B"=>117,"Alpha"=>100), 
       "6"=>array("R"=>92,"G"=>224,"B"=>46,"Alpha"=>100), 
       "7"=>array("R"=>224,"G"=>176,"B"=>46,"Alpha"=>100)); 

print_r的結果$ palette_cycle :

Array ( 
[0] => Array ([0] => Array ([R] => 108 [G] => 157 [B] => 49 [Alpha] => 100)) 
[1] => Array ([1] => Array ([R] => 108 [G] => 157 [B] => 49 [Alpha] => 100)) 
[2] => Array ([2] => Array ([R] => 108 [G] => 157 [B] => 49 [Alpha] => 100)) 
[3] => Array ([3] => Array ([R] => 108 [G] => 157 [B] => 49 [Alpha] => 100)) 
[4] => Array ([4] => Array ([R] => 108 [G] => 157 [B] => 49 [Alpha] => 100)) 
[5] => Array ([5] => Array ([R] => 108 [G] => 157 [B] => 49 [Alpha] => 100))) 

回答

0

試試這個,而不是

foreach($prop_open as $value) { 
array_push($palette_cycle,"$j"=>array("R"=>108,"G"=>157,"B"=>49,"Alpha"=>100)); 
$j++; 
} 

如果沒有,請嘗試

foreach($prop_open as $value) { 
$palette_cycle[$j] = array("R"=>108,"G"=>157,"B"=>49,"Alpha"=>100); 
$j++; 
} 
+0

我使用'for($ i = 0; $ i 108,「G」=> 157,「B」 => 49,「Alpha」=> 100));}'但是,謝謝! – 2012-04-25 16:39:54

+0

np,很高興你知道了 – squarephoenix 2012-04-25 16:40:39

1

也許嘗試使用數組是這樣的:

$j=0; 
$palette_cycle=array(); 
foreach($prop_open as $value) { 
    $palette_cycle[] = array("R"=>108,"G"=>157,"B"=>49,"Alpha"=>100); 
} 

1,使用整數指標,而不是字符串指標 2,不需要array_push

+0

是的,我用推的唯一原因是因爲我記不起另一種方式去做,謝謝! – 2012-04-25 16:44:40

相關問題