phertzog Posted April 9, 2007 Share Posted April 9, 2007 I have what I'm sure is a simple fix for an experienced PHP person. I have a function whose purpose is to return an array of values to be used elsewhere on a page, but I can't seem to get it to ouput the array. (When I try to reference the $territory variable outside the function I get nothing.) Here's my function: function get_territory_names() { $territory = array(); $sql = "SELECT terr_id, name FROM territories"; $result = mysql_query($sql) or die ("Error in query: $sql. " . mysql_error()); while ($row = mysql_fetch_object($result)) { $key = $row->terr_id; $value = $row->name; $territory[$key] = $value; } return $territory; } $territory is supposed to hold about 12 key/value pairs. I've replaced "return $territory" with "var_export($territory)" and see that the array is populated correctly....so I guess my question is just how do I get the array out of the function so I can reference it elsewhere on the page? Any help is very much appreciated!! Link to comment https://forums.phpfreaks.com/topic/46319-newbie-problem-returning-array-value-from-functionhelp/ Share on other sites More sharing options...
wildteen88 Posted April 9, 2007 Share Posted April 9, 2007 Your function returns a value so what you want to do is this when you call your function $my_array = get_territory_names(); $my_array will be passed the value of $territory created in the function. Link to comment https://forums.phpfreaks.com/topic/46319-newbie-problem-returning-array-value-from-functionhelp/#findComment-225348 Share on other sites More sharing options...
phertzog Posted April 9, 2007 Author Share Posted April 9, 2007 That did the trick. Thank you very much!! Link to comment https://forums.phpfreaks.com/topic/46319-newbie-problem-returning-array-value-from-functionhelp/#findComment-225356 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.