
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中如何通过数组变量名称输出变量的值?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1251538.html
微信扫一扫
支付宝扫一扫