AlexTesler Posted October 20, 2014 Share Posted October 20, 2014 Hi people I am new to php and I need your help. I am usig libchart and want to make the bars colored based on value: Original code: $this->setBarColor(array( new Color(42, 71, 1), )); I want to make something like this: $this->setBarColor(array( if ( ( condition1 )) new Color(42, 171, 1), if ( ( condition2 )) new Color(42, 71, 1), if ( ( condition3 )) new Color(42, 71, 211), )); I do not know php syntax yet, so how can I do this? Thank you, Alex Link to comment https://forums.phpfreaks.com/topic/291953-insert-if-into-an-array/ Share on other sites More sharing options...
Barand Posted October 20, 2014 Share Posted October 20, 2014 try if (condition1) { $this->setBarColor(array(new Color(42, 171, 1))); } elseif (condition2) { $this->setBarColor(array(new Color(42, 71, 1))); } elseif (condition3) { $this->setBarColor(array(new Color(42, 71, 211))); } Link to comment https://forums.phpfreaks.com/topic/291953-insert-if-into-an-array/#findComment-1494265 Share on other sites More sharing options...
Ch0cu3r Posted October 20, 2014 Share Posted October 20, 2014 No not quite. The if statements will be outside of the array. You save the new color to a variable and pass that variable to setBarColor(). Example if(condition 1) { $color = new Color(42, 121, 66); // if condition 1 is met set color to this } elseif(condition 2) { $color = new Color(98, 88, 161); // if condition 2 is met set color to this } elseif(condition 3) { $color = new Color(24, 66, 201); // if condition 3 is met set color to this } else { $color = new Color(42, 71, 1); // the default bar color } $this->setBarColor(array($color)); // pass the color to setBarColor method. Link to comment https://forums.phpfreaks.com/topic/291953-insert-if-into-an-array/#findComment-1494266 Share on other sites More sharing options...
ginerjm Posted October 20, 2014 Share Posted October 20, 2014 Use your array like this: array('value1'=>color1,'value2'=>color2,'value3'=>color2); Use it like this: - determine the condition value - retrieve array value with that condition with: $color = array['condition-value'] Link to comment https://forums.phpfreaks.com/topic/291953-insert-if-into-an-array/#findComment-1494267 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.