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
https://forums.phpfreaks.com/topic/67975-solved-getting-information-from-an-array/
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

 

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;

}

 

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.