PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 4, 2022

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

 July 04, 2022     pchart, php     No comments   

Issue

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++;
}   


Answered By - squarephoenix
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing