Jump to content

[SOLVED] Getting information from an array


trink

Recommended Posts

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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;

}

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$_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.");
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.