Search code examples
phppchart

How to correct a foreach loop and get the wanted number of arrays deep?


I'm pushing a single color into an array to override every bargraph color with a single color but its not working as intended, there is something wrong with the arrays.

My Original Question: How to push array to barChart and color override with single color in pChart?

Updated Question: The print_r results show an extra 2 sets of arrays, it should only be an array holding arrays, no deeper, where are these getting added? How do I correct it?

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)); 

This is how my $palette_cycle array should end up looking after its done (but all RGB is the same):

$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 results for $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 ) ) )

Solution

  • try this instead

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

    if not, try

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