我想动态访问多维数组,因为我需要在配置文件中显示完整路径。以下是我知道您可以在某个级别访问该值的几个示例。
echo $results['Data']['MetaAttrListId']['0'];
$string = "MetaAttrListId";
echo $results['Data'][$string]['0'];
但我想要做的是在字符串中提供该区域的完整位置/路径。像这样。
$string = "['Data']['MetaAttrListId']['0']";
echo $results[$string];
访问多维数组的输出。
(
[Data] => Array
(
[MetaTitle] => Array
(
[0] => Vauxhall combo 1.3 cdti in stunning condition low mileage long mot till august
)
[MetaAttrListId] => Array
(
[0] => Posted
[1] => Make
[2] => Model
[3] => Year
[4] => Mileage
[5] => Seller type
[6] => Body type
[7] => Fuel type
[8] => Transmission
[9] => Colour
[10] => Engine size
)
[MetaAttrListValue] => Array
(
[0] => 1 day ago
[1] => Vauxhall
[2] => COMBO
[3] => 2005
[4] => 79000
[5] => Private
[6] => Car Derived Van
[7] => Diesel
[8] => Manual
[9] => Red
[10] => 1248
)
)
[Error] =>
)
答案 0 :(得分:1)
你可能会尝试我前几天建立的这个功能(也受到我现在不能找到的另一个stackoverflow线程的启发,但也有类似的问题)
像value_in($arrayThingy, 'path.to.that.entry')
或value_in($arrayThingy, 'path/to/that/entry', '/')
希望它有所帮助,如果你找到一个,请报告任何失败:)
/**
* value_in
*
* @param mixed $haystack array or object or nested mix of both
* @param string $path path in any token-separated notation
* @param string $token path separator token
* @return mixed resolved value
*/
function value_in($haystack, $path, $token = ".") {
$path = trim($path, $token); // path.to.place
$segments = explode($token, $path); // ["path", "to", "place"]
$remains = $haystack;
foreach ($segments as $segment) {
if (gettype($remains) === 'array' && isset($remains[$segment])) {
$remains = $remains[$segment];
} else if (gettype($remains) === 'object' && isset($remains->$segment)) {
$remains = $remains->$segment;
} else {
return null;
}
}
return $remains;
}