php变量与数组相互转换的方法(extract与compact)
2018-09-22 00:59
本文实例讲述了php变量与数组相互转换的方法。分享给大家供大家参考,具体如下:
在php中数组与变量相互转换我们可使用到extract或compact函数,这里就来给大家分析一下这两个函数的用法。
compact 多个变量转数组
<?php //多个变量转数组 $info=compact(name,email);//传递变量名 print_r($info); /* Array ( [name] => jb51 [email] => jb51@jb51.net ) */ ?>
extract 数组转多个变量
<?php //数组转多个变量 $capitalcities[England] = London; $capitalcities[Scotland] = Edinburgh; $capitalcities[Wales] = Cardiff; extract($capitalcities);//转变成三个变量 England,Scotland,Wales print $Wales;//Cardiff ?>
例:
<?php $my_array = array(a => Cat,b => Dog, c => Horse); extract($my_array); echo $a = $a; $b = $b; $c = $c; ?>
结果:
$a = Cat; $b = Dog; $c = Horse
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php常用函数与技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP错误与异常处理方法总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
文章标题:php变量与数组相互转换的方法(extract与compact)
文章链接:http://soscw.com/essay/16990.html