Blogger Information
Blog 55
fans 0
comment 0
visits 50468
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP数组与字符串,变量之间的转换-0824
Bean_sproul
Original
1011 people have browsed it

list() 把数组中的元素转为变量: 用在索引数组上

extract() 关联数组转为变量

compact(): 将变量转为关联数组

explode():将字符串转换数组

implode():拼接

实例

<?php 
header("Content-Type: text/html;charset=utf8");
echo '<h1>数组与变量与字符串之间的转换</h1>';
echo '<hr>';
echo '<h2>1.list() 把数组中的元素转为变量: 用在索引数组上</h2>';

list($name,$age,$money) = ['小红',18,2000];
echo $name,',',$age,',',$money;
 
echo '<hr>';

echo '<h2>2.extract() 关联数组转为变量</h2>';
$arr = ['id'=>1,'name'=>'小红','money'=>5000 ];
//extract()返回变量的数量
echo '共有:', var_export(extract($arr),'ture'),'个变量<br>';

echo '我的id:'.$id .',我的名字:'.$name.',我的工资:'.$money;

echo '<hr>';
echo '<pre>';
echo '<h2>3.compact(): 将变量转为关联数组</h2>';
$big='大';
$small='小';
$middle='中';
$arr = compact('big','small','middle');
var_dump($arr);

echo '<hr>';

echo '<h2>4.explode():将字符串转换数组</h2>';
$string = 'a,b,c,d,e,f';
$arr = explode(',', $string);
echo var_export($arr);
//里面可以添加数字 如:2 就是前面1个,由2个扎堆放一起  -2 就是最后二个过滤
echo var_export(explode(',', $string,-2)),'<br>';
echo var_export(explode(',',$string,2)), '<br>';

echo '<hr>';

echo '<h2>5.implode($glue, $arr):拼接</h2>';
$arr = ['首页','公司新闻','公司新闻','联系我们'];
//echo var_export(implode($arr),true),'<br>';
echo var_export(implode('|',$arr),true),'<br>';
//添加<a>转为导航
echo var_export('<a href="#">'.implode('</a>|<a href="#">',$arr).'</a>'),'<br>';



?>

运行实例 »

点击 "运行实例" 按钮查看在线实例

 

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post