SitePoint Sponsor |
|
User Tag List
Results 1 to 12 of 12
Hybrid View
-
Apr 14, 2009, 07:51 #1
- Join Date
- Apr 2004
- Location
- Boston
- Posts
- 482
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Multidimensional array function from seperate arrays
Hello,
I've tried to get help on this but a bit prematurely so I am trying again.
I have 2 arrays but I need to make them into a multidimensional array but the factors need to change using IF statements and I need this all done in a function for callback in Jpgraph.
Here is my code:
PHP Code://the following 2 arrays are pulled from my DB and work
//determine size
$cirsz[0] = 'small';
$cirsz[1] = 'medium';
$cirsz[2] = 'large';
//determine color from these values
$circlr[0] = 23;
$circlr[1] = 20;
$circlr[2] = 18;
//breaksdown somewhere in the function
function SettingSet($cirsz, $circlr) {
global $cirsz;
global $circlr;
// This callback will adjust the fill color and size of
// the datapoint according to the data value
$i = 0;
//get sizes
foreach($cirsz as $s){
if($s == 'small'){
$sz = 15;
}elseif($s == 'medium'){
$sz = 30;
}else{
$sz = 50;
}
if($circlr <= 25 && ($circlr >= 18.75)){
$col = "red";
}elseif($circlr < 18.75 && ($circlr >= 12.5)){
$col ="orange";
}elseif($circlr < 12.5 && ($circlr >= 6.25)){
$col = "yellow";
}else{
$col = "green";
}
$i++;
}
return array($sz,"",$col);
}
Code:array( $a[0] = 15 => "" => red; $a[1] = 30 => "" => red; $a[2] = 50 => "" => orange; }
-
Apr 14, 2009, 07:58 #2PHP Code:
function SettingSet($cirsz, $circlr) {
// This callback will adjust the fill color and size of
// the datapoint according to the data value
$result = array();
//get sizes
foreach ($cirsz as $s) {
if($s == 'small') {
$sz = 15;
} elseif ($s == 'medium') {
$sz = 30;
} else {
$sz = 50;
}
if ($circlr <= 25 && ($circlr >= 18.75)) {
$col = "red";
} elseif ($circlr < 18.75 && ($circlr >= 12.5)) {
$col ="orange";
} elseif ($circlr < 12.5 && ($circlr >= 6.25)) {
$col = "yellow";
} else {
$col = "green";
}
$result[] = array($sz, "", $col);
}
return $result;
}
Guido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
Apr 14, 2009, 08:06 #3
- Join Date
- Apr 2004
- Location
- Boston
- Posts
- 482
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Apr 14, 2009, 08:51 #4
- Join Date
- Apr 2004
- Location
- Boston
- Posts
- 482
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I think I figured out the problem but I'm not sure how to fix it.
This:
PHP Code:$result[] = array($sz, "", $col);
Any ideas?
-
Apr 14, 2009, 08:58 #5
- Join Date
- Jul 2008
- Posts
- 5,757
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You should use var_dump() or print_r() if you want to view the contents of a variable, or the return value of a function. echo/print isn't suited to displaying arrays.
This is probably a good time for you to read a tutorial on php functions.
-
Apr 14, 2009, 09:15 #6
- Join Date
- Apr 2004
- Location
- Boston
- Posts
- 482
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I have used all of the above. They all return as either NULL or nothing at all. If i echo $sz or $col within the function they return correctly for each increment of the respective arrays. I am also getting this error when i print_r($result) or callback the function: Fatal error: Unsupported operand types
-
Apr 14, 2009, 09:48 #7
- Join Date
- Jul 2008
- Posts
- 5,757
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
post some code which demonstrates your problem.
-
Apr 14, 2009, 10:13 #8
- Join Date
- Apr 2004
- Location
- Boston
- Posts
- 482
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I've made a few adjustments and now I have the array printing correctly when it's called back inside the function. When it is called from without using
print_r($result); It returns nothing just whitespace.
Using:
print SettingSet(); it calls back the word Array.
Code is below:
PHP Code:function SettingSet() {
// This callback will adjust the fill color and size of
// the datapoint according to the data value
global $cirsz;
global $circlr;
//$result = array();
//get sizes
$i=0;
foreach ($cirsz as $s) {
if($s == 'small') {
$sz = 15;
} elseif ($s == 'medium') {
$sz = 30;
} else {
$sz = 50;
}
//$sizefin = array($sz);
if ($circlr[$i] <= 25 && ($circlr[$i] >= 18.75)) {
$col = "red";
} elseif ($circlr[$i] < 18.75 && ($circlr[$i] >= 12.5)) {
$col ="orange";
} elseif ($circlr[$i] < 12.5 && ($circlr[$i] >= 6.25)) {
$col = "yellow";
} else {
$col = "green";
}
// $colfin = array($col);
//$b = "";
// $bl = array($b);
$i++;
$result[] = array($sz, "", $col);
}
//print_r($result); //uncommenting this line prints the array correctly.
return $result;
}
-
Apr 14, 2009, 10:18 #9
- Join Date
- Jul 2008
- Posts
- 5,757
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
print and echo are not suitable for displaying an array. You should use print_r() or var_dump() to display an array.
Your function returns an array.
PHP Code:print_r(SettingSet());
-
Apr 14, 2009, 10:30 #10
- Join Date
- Apr 2004
- Location
- Boston
- Posts
- 482
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Apr 14, 2009, 10:35 #11
- Join Date
- Jul 2008
- Posts
- 5,757
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The error message probably tells you where the error is(file and line)
-
Apr 14, 2009, 10:54 #12
- Join Date
- Apr 2004
- Location
- Boston
- Posts
- 482
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
it doesn't there aren't even close to 306 lines of code. Which is where it says the error is. I think it's something to do with the way the array is called back but I'm really stumped at this point.
It was originally built from this function they give as an example in jpgraph.
PHP Code:function FCallback($aVal) {
// This callback will adjust the fill color and size of
// the datapoint according to the data value according to
if( $aVal < 30 ) $c = "blue";
elseif( $aVal < 70 ) $c = "green";
else $c="red";
return array(floor($aVal/3),"",$c);
}
Bookmarks