Page 1 of 1
How to use an Array returned from a Function
Posted: Fri Jul 26, 2002 7:04 am
by riley
I have a function that is returning an array from a function do not know the syntax to use it correctly. I am returning an array of colors to use at various places.
Here is the function:
[syntax=php]ColorShotVal('Ltch',$AvgLtch, $AvgFlag, $AvgVini)[/syntax]
Here is how I am using it without returning an array (single value)
[syntax=php]<Font Color='".ColorShotVal('Ltch',$AvgLtch, $AvgFlag, $AvgVini)."'>[/syntax]
I want to return an array with fontcolor just be one of the returned values so I have tried ColorShotVal('Ltch',$AvgLtch, $AvgFlag, $AvgVini)['fntCol'] but am getting a parse error (unepected "[").
Posted: Fri Jul 26, 2002 7:12 am
by twigletmac
Code: Select all
ColorShotVal('Ltch',$AvgLtch, $AvgFlag, $AvgVini)
is not a function, it is a call to a function.
If you want to change the information returned by a function you have to edit the function - not the call. What is the code for ColorShotVal()?
You can probably save the returned value from the function as a variable, if the value is indeed returned and not just echoed from the function.
Try:
Code: Select all
$colorї'fntCol'] = ColorShotVal('Ltch',$AvgLtch, $AvgFlag, $AvgVini);
and have a read of this:
http://www.php.net/manual/en/functions.php
http://www.php.net/manual/en/functions. ... values.php
Mac
Posted: Fri Jul 26, 2002 7:14 am
by volka
you have to assign the value first to a variable

$arr = ......
hmmm....twigletmac was faster (Topic review is a fine thing

)
Posted: Fri Jul 26, 2002 7:24 am
by riley
Here's a portion the Function:
Code: Select all
FUNCTION ColorShotVal($Shot, $Val, $Flag, $Vini){
Switch ($Shot){
CASE 'Ltch';
$aCol = array('bgColor' => '#EFEFEF' ,'fontColor' => '#000000' );
Return $aCol;
break;
I have multiple lines that are posted from a record set with a loop. Here are two.
Code: Select all
<TD Class='datacol'><Font Color='".ColorShotVal('Ltch',$AvgLtch, $AvgFlag, $AvgVini)."'>$AvgLtch </Font></TD>
<TD Class='datacol'><Font Color='".ColorShotVal('Bffr',$AvgBffr, $AvgFlag, $AvgVini)."'>$AvgBffr </Font></TD>
Posted: Fri Jul 26, 2002 2:33 pm
by gnu2php
Since the function returns an array, I think you need to have a variable get the function's return value.
What about something like this:
Code: Select all
$ltch_color = ColorShotVal('Ltch',$AvgLtch, $AvgFlag, $AvgVini);
$bffr_color = ColorShotVal('Bffr',$AvgBffr, $AvgFlag, $AvgVini);
And then use the 'fontColor' keys in the arrays (I haven't tested this):
Code: Select all
echo "<TD Class='datacol'><Font Color='".$ltch_colorї'fontColor']."'>$AvgLtch </Font></TD>";
echo "<TD Class='datacol'><Font Color='".$bffr_colorї'fontColor']."'>$AvgBffr </Font></TD>";