Php 多维数组的动态变量名
我左手上有一把“钥匙”:Php 多维数组的动态变量名,php,arrays,recursion,dynamic-programming,Php,Arrays,Recursion,Dynamic Programming,我左手上有一把“钥匙”: Localization.address.commune.id 还有很多其他的值,比如这个值,它们是动态的(我不能在我的代码中直接使用它们,因为我不知道它们会是什么) 另一方面,我有一个这样的数组(来自json解码): 我无法列出所有要利用的“密钥”,因此我需要自动获取$object['localization']['adresse']['commune']['id']的值 我尝试过这个,但不起作用: $test['localisation']['adresse']['
Localization.address.commune.id
还有很多其他的值,比如这个值,它们是动态的(我不能在我的代码中直接使用它们,因为我不知道它们会是什么)
另一方面,我有一个这样的数组(来自json解码):
我无法列出所有要利用的“密钥”,因此我需要自动获取$object['localization']['adresse']['commune']['id']的值
我尝试过这个,但不起作用:
$test['localisation']['adresse']['commune']['id'] = 16418 ;
$var = '$test[\'localisation\'][\'adresse\'][\'commune\'][\'id\']' ;
echo $var ; // $test['localisation']['adresse']['commune']['id']
var_dump($$var) ; // NULL Notice: Undefined variable: $test['localisation']['adresse']['commune']['id']
var_dump(${$var}) ; // NULL Notice: Undefined variable: $test['localisation']['adresse']['commune']['id']
我想它是在寻找一个复杂名称的简单变量,而不是一个多维数组,但我不知道如何才能做到
谢谢你的帮助 试试这个:
$str = '{
"localisation" : {
"adresse" : {
"adresse1" : "Le Chatelard",
"codePostal" : "42820",
"etat" : "France",
"commune" : {
"id" : 16418
}
}
}
}
';
$data = json_decode($str, true);
$var = $data['localisation']['adresse']['commune']['id'] ;
echo $var ;
print_r($data);
除了遍历数组并尝试在内部数组中查找键(如果有的话),我看不到其他方法 我提出了两种变体:递归和迭代。它们还将处理键和数组的“深度”不同的情况,例如,如果
$key
包含的元素多于数组的深度,则将返回NULL
,如果小于,则返回最后一个键下的任何元素
递归变量
迭代变量
您需要用json对数组中的json字符串进行解码,我已经用\GuzzleHttp\Utils::jsonDecode($objet,true);。这不是真正的问题,我可以从问题中删除json部分,我已经有了一个关联数组:
array([Localization]=>array([adresse]=>array([adresse1]=>LeChatelard[codePostal]=>42820[etat]=>France[commune]=>array([id]=>16418)))
对不起,我想我的问题有错。我对将json解码到数组中没有任何问题:我的问题是我不能用PHP编写$data['localization']['adresse']['commune']['id']
,因为我不知道这些值:它们是动态的。它可能是Localization.geoloc.gps.latitude或information.description或services.room。。。或者别的什么。在php中转换json实际上不是这里的问题,sorryOK。我得到了您想要的,我的建议是,与其创建动态变量,不如用“.”分割$keys值,然后用它直接访问关联数组中的值。因为我不知道是否有4个或更多的键。可能是信息服务键3键4键5。。。因此,将其转换为['information']['services']['key3']会更舒适一些。。。等等,然后尝试访问它,而不是创建一个递归函数。对于一大组值来说,这也可能是一个很大的性能问题。非常感谢您的帮助,我想我要买一个看起来正是我想要的:D thans!
$str = '{
"localisation" : {
"adresse" : {
"adresse1" : "Le Chatelard",
"codePostal" : "42820",
"etat" : "France",
"commune" : {
"id" : 16418
}
}
}
}
';
$data = json_decode($str, true);
$var = $data['localisation']['adresse']['commune']['id'] ;
echo $var ;
print_r($data);
$a = [
'localisation' => [
'adresse' => [
'adresse1' => 'Le Chatelard',
'codePostal' => 42820,
'etat' => 'France',
'commune' => [
'id' => 16418,
],
],
],
];
$key = 'localisation.adresse.commune.id';
function getValueByKeyRecursively($a, $key)
{
$keyList = explode('.', $key);
$currentKey = array_shift($keyList);
// Found the value
if (empty($currentKey)) {
return $a;
}
// No more depth to traverse or no such key
if (!is_array($a) || !array_key_exists($currentKey, $a)) {
return null;
}
return getValueByKeyRecursively($a[$currentKey], implode('.', $keyList));
}
var_dump(getValueByKeyRecursively($a, $key)); // outputs: int(16418)
function getValueByKey(array $a, $key)
{
$keyList = explode('.', $key);
$returnValue = $a;
do {
$currentKey = array_shift($keyList);
// Found the value
if ($currentKey === null) {
break;
}
// No more depth to traverse or no such key
if (!is_array($returnValue) || !array_key_exists($currentKey, $returnValue)) {
return null;
}
$returnValue = $returnValue[$currentKey];
} while (true);
return $returnValue;
}
var_dump(getValueByKey($a, $key)); // outputs: int(16418)