在普通数组中你可以这样选择
$key='example';
echo $array[$key];
多维情况如何?
$keys='example[secondDimension][thirdDimension]';
echo $array[$keys];
解决这个问题的正确方法是什么?
我认为这个解决方案很好。
注意你必须用“[”和“]”包裹所有的键。
$array = array(
'example' => array(
'secondDimension' => array(
'thirdDimension' => 'Hello from 3rd dimension',
)
),
);
function array_get_value_from_plain_keys($array, $keys)
{
$result;
$keys = str_replace(array('[', ']'), array("['", "']"), $keys); // wrapping with "'" (single qoutes)
eval('$result = $array' . $keys . ';');
return $result;
}
$keys = '[example][secondDimension][thirdDimension]'; // wrap 1st key with "[" and "]"
echo array_get_value_from_plain_keys($array, $keys);
了解有关 eval() 函数的更多信息
如果您还想检查该值是否已定义,则可以使用此功能
function array_check_is_value_set_from_plain_keys($array, $keys)
{
$result;
$keys = str_replace(array('[', ']'), array("['", "']"), $keys); // wrapping with "'" (single qoutes)
eval('$result = isset($array' . $keys . ');');
return $result;
}
给那个函数一个更好的名字将不胜感激^^
这里有一个不使用 eval 的解决方案:
$array = [
'example' => [
'secondDimension' => [
'thirdDimension' => 'Hello from 3rd dimension',
],
],
];
$keys = '[example][secondDimension][thirdDimension]';
function get_array_value( $array, $keys ) {
$keys = explode( '][', trim( $keys, '[]' ) );
return get_recursive_array_value( $array, $keys );
}
function get_recursive_array_value( $array, $keys ) {
if ( ! isset( $array[ $keys[0] ] ) ) {
return null;
};
$res = $array[ $keys[0] ];
if ( count( $keys ) > 1 ) {
array_shift( $keys );
return get_recursive_array_value( $res, $keys );
} else {
return $res;
}
}
echo get_array_value( $array, $keys );
希望你使用$b数组跟随$a数组的嵌套键并获取$c中的值?
<?php
$a = [ 'key_1' => [ 'key_2' => [ 'key_3' => 'value', ], ], ] ;
$b = ['key_1', 'key_2', 'key_3', ] ;
if ($b)
{
$c = $a ; // $a is not copied (copy-on-write)
foreach($b as $k)
if (isset($c[$k]))
$c = $c[$k] ;
else
{
unset($c);
break;
}
var_dump($c);
}
/*
output :
string(5) "value"
*/
或者您想为任意形成的字符串生成 $b 数组并获取 $c 作为参考?
<?php
$a = [ 'key_1' => [ 'key_2' => [ 'key_3' => 'value', ], ], ] ;
$b = '[key_1][key_2][key_3]';
if ($b !== '')
{
$b = explode('][', trim($b, '[]'));
$c = &$a ;
foreach($b as $k)
if (isset($c[$k]))
$c = &$c[$k] ;
else
{
unset($c);
break;
}
}
var_dump($c);
$c = 'new val' ;
unset($c);
var_dump($a['key_1']['key_2']['key_3']);
/*
output :
string(5) "value"
string(7) "new val"
*/
我编写的使用 eval() 的简化示例,用于从 Shopify 客户数据数组中提取电话号码:
// this is weird but whatever
$arrayPaths = array(
'[\'phone\']',
'[\'addresses\'][0][\'phone\']',
'[\'addresses\'][1][\'phone\']'
);
foreach ($arrayPaths as $path) {
// hack to store/access multidimentional array keys as array, sets
// a variable named $phoneNumber
$code = '$phoneNumber = $dataArray' . $path . ';';
eval($code);
var_dump($phoneNumber);
}
你必须为数组的每个维度使用一个单独的变量。您在多维数组中看到的一个常见模式是,您需要对二维数组做一些事情是这样的:
$pets = [
'dog' => ['Jack', 'Fido', 'Woofie'],
'cat' => ['Muggles', 'Snowball', 'Kitty'],
];
// Loop through keys in first dimension
foreach ($pets as $type => $names) {
foreach ($names as $index => $name) {
// And do something with second dimension using the variable
// you've gained access to in the foreach
$pets[$type][$index] = strtoupper($name);
}
}