trink Posted September 5, 2007 Share Posted September 5, 2007 I've got a problem with an array, and I'm not sure exactly why my script isn't working. I'm pretty new with arrays, so that could be the reason right there. My script look like this function getconfig($getkey){ return $currentconfig[$getkey]; } In the array, the online key is set to 1. I am trying to get the 'online' key, so I use getconfig('online'); but it doesn't return anything. If I use print $currentconfig['online']; it prints "1". Any tips? Thanks a lot. Quote Link to comment Share on other sites More sharing options...
teng84 Posted September 5, 2007 Share Posted September 5, 2007 no value for this $currentconfig[$getkey]; so it wont return anything should be $currentconfig[$getkey]='test'; ok ill explain $currentconfig <--your array [$getkey]<--your index 'test'<-- the value of that array on that specific index Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted September 5, 2007 Share Posted September 5, 2007 why do you need a function if you are just gonna say $value = getconfig($key); you can easily just say $value = $myarray[$key]; Same thing and saves you a step, now if that is an undefined key then you have an issue, so maybe the function might help you by doing something like function getconfig($key){ if(!empty($myarray[$key])){ return $myarray[$key]; } else{ return FALSE; } Quote Link to comment Share on other sites More sharing options...
btherl Posted September 5, 2007 Share Posted September 5, 2007 Try this function: function getconfig($getkey){ global $currentconfig; return $currentconfig[$getkey]; } The problem may be that global variables are not available by default within a function Quote Link to comment Share on other sites More sharing options...
trink Posted September 5, 2007 Author Share Posted September 5, 2007 global $currentconfig; worked. Thanks! Quote Link to comment Share on other sites More sharing options...
trink Posted September 5, 2007 Author Share Posted September 5, 2007 Alright, new problem. I've done all that and whatnot, and changed the name to $_CONFIG. So it looks like this. $_CONFIG=array(); global $_CONFIG; $loadconfig=@file('config.php'); foreach($loadconfig as $config){ $configkey=substr(trim($config),0,strpos($config,'=')); $configval=substr(trim($config),(strpos($config,'=')+1)); $_CONFIG[$configkey]=$configval; } When I run it though a foreach or a while list each loop, it says its not an array... When I get data from it, such as $_CONFIG['online'], or when I print_r it, it rerturns data like an array. The foreach loop is as such: foreach($_CONFIG as $configchangekey=>$configchangevalue){ //stuff goes here } Any tips on this problem? Thanks Quote Link to comment Share on other sites More sharing options...
teng84 Posted September 5, 2007 Share Posted September 5, 2007 are you sure this line is successful $loadconfig=@file('config.php'); remove the @ and see if theres an error Quote Link to comment Share on other sites More sharing options...
trink Posted September 5, 2007 Author Share Posted September 5, 2007 Yes, it loads correctly, because when I check the array with print $_CONFIG['online'] it returns 1. The problem lies somewhere else, is maybe "global $_CONFIG;" setting it as a non-array variable? Quote Link to comment Share on other sites More sharing options...
btherl Posted September 5, 2007 Share Posted September 5, 2007 Please post your full code. If your code is too large, post a smaller script that replicates the problem. Quote Link to comment Share on other sites More sharing options...
trink Posted September 5, 2007 Author Share Posted September 5, 2007 $_CONFIG=array(); global $_CONFIG; $loadconfig=@file('config.php'); foreach($loadconfig as $config){ $configkey=substr(trim($config),0,strpos($config,'=')); $configval=substr(trim($config),(strpos($config,'=')+1)); $_CONFIG[$configkey]=$configval; } function getconfig($getkey){ return $_CONFIG[$getkey]; } function changeconfig($changekey,$changevalue){ foreach($_CONFIG as $configchangekey=>$configchangevalue){ if ($configchangekey == $changekey){ $changeconfig.=$configchangekey.'='.$changevalue.chr(10); }else{ $changeconfig.=$configchangekey.'='.$configchangevalue.chr(10); } } fwrite($changewriteconfig=fopen('config.php','w'),trim($changeconfig)) && fclose($changewriteconfig); } if(!$_CONFIG['online']){ die("Not online."); } Quote Link to comment Share on other sites More sharing options...
btherl Posted September 5, 2007 Share Posted September 5, 2007 You must put the "global $_CONFIG"; inside EVERY function which accesses $_CONFIG. This is different behaviour from some other languages. Quote Link to comment Share on other sites More sharing options...
trink Posted September 5, 2007 Author Share Posted September 5, 2007 Alright, that worked, thanks a lot! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.