源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

php变量与数组相互转换的方法(extract与compact)

  • 时间:2021-10-22 21:01 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:php变量与数组相互转换的方法(extract与compact)
本文实例讲述了php变量与数组相互转换的方法。分享给大家供大家参考,具体如下: 在php中数组与变量相互转换我们可使用到extract或compact函数,这里就来给大家分析一下这两个函数的用法。 [b]compact 多个变量转数组[/b]
<?php
  //多个变量转数组
  $name='jb51';
  $email='jb51@jb51.net';
  $info=compact('name','email');//传递变量名
  print_r($info);
  /*
  Array
  (
    [name] => jb51
    [email] => jb51@jb51.net
  )
  */
?>

[b]extract 数组转多个变量[/b]
<?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相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/168.htm]php常用函数与技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/623.htm]PHP数组(Array)操作技巧大全[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》、《[url=http://www.1sucai.cn/Special/196.htm]PHP错误与异常处理方法总结[/url]》、《[url=http://www.1sucai.cn/Special/348.htm]PHP基本语法入门教程[/url]》、《[url=http://www.1sucai.cn/Special/43.htm]php面向对象程序设计入门教程[/url]》、《[url=http://www.1sucai.cn/Special/84.htm]php+mysql数据库操作入门教程[/url]》及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》 希望本文所述对大家PHP程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部