
php 实现输出数组变量
在 php 中,如果要通过数组中的变量名称输出变量的值,可以使用以下几种方法:
方法 1:使用 gettype() 函数
$html['result'] = '1111111111';
$arr = [
'urls' => "html['result']"
];
$temp = $arr['urls'];
$urls = $html['result'];
echo $$temp; // 输出 "1111111111"方法 2:使用 eval() 函数
立即学习“PHP免费学习笔记(深入)”;
$html['result'] = '1111111111';
$arr = [
'urls' => "\$html['result']"
];
$temp = $arr['urls'];
eval("\$temp = $temp;");
echo $temp; // 输出结果:"1111111111"方法 3:使用 反射类**
$html['result'] = '1111111111';
$arr = [
'urls' => 'html'
];
$temp = null;
// 使用反射获取数组的值
$reflection = new reflectionclass($arr['urls']);
$property = $reflection->getproperty('result');
$temp = $property->getvalue($html);
echo $temp;方法 4:使用 自定义函数**
$html['result'] = '1111111111';
$arr = [
'urls' => "html['result']"
];
$temp = $arr['urls'];
function getValue($str) {
global $html;
preg_match("/\['(.*)'\]/", $str, $matches);
return $html[$matches[1]];
}
echo getValue($temp); // 输出 "1111111111"以上就是PHP中如何通过数组变量名称输出变量的值?的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号