How to use an Array returned from a Function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
riley
Forum Commoner
Posts: 45
Joined: Thu May 02, 2002 6:31 pm

How to use an Array returned from a Function

Post 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 "[").
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you have to assign the value first to a variable :(
$arr = ......

hmmm....twigletmac was faster (Topic review is a fine thing :D )
User avatar
riley
Forum Commoner
Posts: 45
Joined: Thu May 02, 2002 6:31 pm

Post 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>
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post 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>";
Post Reply