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&#1111;'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 :D )

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)&#123;

  Switch ($Shot)&#123;
    	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&nbsp;</Font></TD>
							 <TD Class='datacol'><Font Color='".ColorShotVal('Bffr',$AvgBffr, $AvgFlag, $AvgVini)."'>$AvgBffr&nbsp;</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&#1111;'fontColor']."'>$AvgLtch&nbsp;</Font></TD>";
echo "<TD Class='datacol'><Font Color='".$bffr_color&#1111;'fontColor']."'>$AvgBffr&nbsp;</Font></TD>";