PHP函数,可以从数组键返回动态级别的值
我想使用PHP编写一个函数,实现以下伪代码所示的功能:PHP函数,可以从数组键返回动态级别的值,php,arrays,dynamic,Php,Arrays,Dynamic,我想使用PHP编写一个函数,实现以下伪代码所示的功能: function return_value($input_string='array:subArray:arrayKey') { $segments = explode(':',$input_string); $array_depth = count(segments) - 1; //Now the bit I'm not sure about //I need to dynamically genera
function return_value($input_string='array:subArray:arrayKey')
{
$segments = explode(':',$input_string);
$array_depth = count(segments) - 1;
//Now the bit I'm not sure about
//I need to dynamically generate X number of square brackets to get the value
//So that I'm left with the below:
return $array[$subArray][$arrayKey];
}
上述情况是否可能?我真的很想知道如何实现它。您可以使用并向下走到所述键的级别
function get_array_element($key, $array)
{
if(stripos(($key,':') !== FALSE) {
$currentKey = substr($key,0,stripos($key,':'));
$remainingKeys = substr($key,stripos($key,':')+1);
if(array_key_exists($currentKey,$array)) {
return ($remainingKeys,$array[$currentKey]);
}
else {
// handle error
return null;
}
}
elseif(array_key_exists($key,$array)) {
return $array[$key];
}
else {
//handle error
return null;
}
}
您可以使用递归函数(或者它的迭代等价物,因为它是尾部递归): 通过一个物体 假设我们希望这是一个API,只传递一个字符串(请记住HTTP在这方面有一些方法限制,您可能需要发布字符串而不是GET) 字符串需要同时包含数组数据和“键”位置。最好先发送密钥,然后发送数组:
function decodeJSONblob($input) {
// Step 1: extract the key address. We do this is a dirty way,
// exploiting the fact that a serialized array starts with
// a:<NUMBEROFITEMS>:{ and there will be no "{" in the key address.
$n = strpos($input, ':{');
$items = explode(':', substr($input, 0, $n));
// The last two items of $items will be "a" and "NUMBEROFITEMS"
$ni = array_pop($items);
if ("a" != ($a = array_pop($items))) {
die("Something strange at offset $n, expecting 'a', found {$a}");
}
$array = unserialize("a:{$ni}:".substr($input, $n+1));
while (!empty($items)) {
$key = array_shift($items);
if (!array_key_exists($key, $array)) {
// there is not this item in the array.
}
if (!is_array($array[$key])) {
// Error.
}
$array = $array[$key];
}
return $array;
}
$arr = array(
0 => array(
'hello' => array(
'joe','jack',
array('jill')
)));
print decodeJSONblob("0:hello:1:" . serialize($arr));
print decodeJSONblob("0:hello:2:0" . serialize($arr));
在请求
0:hello:2:
时,会得到一个数组{0:'jill'}
使用如下递归函数或使用数组键引用的循环
<?php
function lookup($array,$lookup){
if(!is_array($lookup)){
$lookup=explode(":",$lookup);
}
$key = array_shift($lookup);
if(!isset($array[$key])){
//throw exception if key is not found so false values can also be looked up
throw new Exception("Key does not exist");
}else{
$val = $array[$key];
if(count($lookup)){
return lookup($val,$lookup);
}
return $val;
}
}
$config = array(
'db'=>array(
'host'=>'localhost',
'user'=>'user',
'pass'=>'pass'
),
'data'=>array(
'test1'=>'test1',
'test2'=>array(
'nested'=>'foo'
)
)
);
echo "Host: ".lookup($config,'db:host')."\n";
echo "User: ".lookup($config,'db:user')."\n";
echo "More levels: ".lookup($config,'data:test2:nested')."\n";
这听起来像是一个错误。为什么不能使用
serialize
或json\u encode
?我正在尝试构建一个全局可用的静态配置类,在这个类中,我可以使用类似于config::get('db:server')的语法来检索值。
这要求我传入一个数组和一个字符串,但我希望能够只传入一个字符串。例如返回值('array:subArray:arrayKey')
。有没有一种方法可以调整你的函数以适应这种情况?你需要数组是全局的。否则函数如何访问阵列?或者您可以发送以某种方式编码的数组和密钥序列(例如序列化)。
jack
jill
<?php
function lookup($array,$lookup){
if(!is_array($lookup)){
$lookup=explode(":",$lookup);
}
$key = array_shift($lookup);
if(!isset($array[$key])){
//throw exception if key is not found so false values can also be looked up
throw new Exception("Key does not exist");
}else{
$val = $array[$key];
if(count($lookup)){
return lookup($val,$lookup);
}
return $val;
}
}
$config = array(
'db'=>array(
'host'=>'localhost',
'user'=>'user',
'pass'=>'pass'
),
'data'=>array(
'test1'=>'test1',
'test2'=>array(
'nested'=>'foo'
)
)
);
echo "Host: ".lookup($config,'db:host')."\n";
echo "User: ".lookup($config,'db:user')."\n";
echo "More levels: ".lookup($config,'data:test2:nested')."\n";
Host: localhost
User: user
More levels: foo