I've got some arrays like this$something = array('foo' => 'bar');
Now how can I get the content of $something
? I want to use this method to retrieve values from arrays but can't work out how to find an array with only it's name given as a string.
getArrayData($array,$key){
// $array == 'something';
// $key == 'foo';
// this should return 'bar'
}
EDIT:
I abstracted this too much maybe, so here is the full code:
class Config {
public static $site = array(
'ssl' => 'true',
'charset' => 'utf-8',
// ...
);
public static $menu = array(
'home' => '/home',
'/hub' => '/hub',
// ...
);
public static function get($from, $key){
return self::$from[$key];
}
public static function __callStatic($method, $key){
return self::get($method,$key);
}
}
In the end the configuration should be accessible from within the whole app by using Config::site('charset')
to return 'utf-8'
You should pass the array itself as parameter instead of the name. Then you can just return the value by the given key:
function getArrayData($array,$key){
return $array[$key];
}
$array == 'something'
doesn't mean much, you can easily check the array keys and return the value if the key exists:
function getArrayData($array,$key){
if(isset($array[$key])) return $array[$key];
else return "";
}
You can use Variable-Variables
<?php
$something = array('foo' => 'bar');
$key="foo";
$arrayName="something";
echo getArrayData($$arrayName,$key); // Notice the use of $$
function getArrayData($array,$key){
return isset($array[$key])? $array[$key] : NULL ;
}