Jump to content


Photo

"passing" arrays


  • Please log in to reply
2 replies to this topic

#1 kalivos

kalivos
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationCalifornia

Posted 17 July 2006 - 06:17 PM

I'm trying to declare two arrays in a function, then call that function from another function. It seems that the second function will not use these arrays. How do I get them to "pass"?

-- Sample code --

function function_a($type) 
{
	$array_1 = array("Value1", "Value2");
	$array_2 = array("Value100", "Value200");
	
        // following is an attempt to return an array. Incorrect way of doing it?
	if($type == "some_value")
	{
		return $array_1;
	}
	elseif ($type == "other_value")
	{
		return $array_2;
	}
}

function function_b()
{

        function_a("other_value");
        print_r($array_2);

}

Thanks in advance,
-Kalivos
Make a donation to phpfreaks. They need your help![br][br]

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 17 July 2006 - 06:25 PM

Do this instead:
function function_b()
{
    print_r(function_a("other_value"));
}

return does return the variable, $array_2, but the value of $array_2.

#3 kalivos

kalivos
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationCalifornia

Posted 17 July 2006 - 06:32 PM

Ahh, works. Thanks!
Make a donation to phpfreaks. They need your help![br][br]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users