本站资源收集于互联网,不提供软件存储服务,每天免费更新优质的软件以及学习资源!

PHP中如何通过数组变量名称输出变量的值?

网络教程 app 1℃

PHP中如何通过数组变量名称输出变量的值

php 实现输出数组变量

在 php 中,如果要通过数组中的变量名称输出变量的值,可以使用以下几种方法:

方法 1:使用 gettype() 函数

$html[‘result’] = ‘1111111111’;$arr = [ ‘urls’ => "html[‘result’]"];$temp = $arr[‘urls’];$urls = $html[‘result’];echo $$temp; // 输出 "1111111111"

方法 2:使用 eval() 函数

$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中如何通过数组变量名称输出变量的值?

喜欢 (0)