Blogger Information
Blog 20
fans 0
comment 0
visits 14713
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数组与字符串,变量之间的转换 2018.8.24
李逍遥
Original
815 people have browsed it

实例

<?php
//echo '<pre>';
//数组、变量、字符串之间的转换
//1.list() 把索引数组中的元素转换为变量
list($name,$height,$age) = ['Zenlee','178cm',24];
echo '姓名:',$name,' 身高:',$height,' 年龄:',$age,'<hr>';

//2.extract() 把关联数组中的元素的值转换为变量
$ary = ['sex'=>'man','country'=>'china'];
echo '变量数:',var_export(extract($ary)),'<br>';
echo '性别:',$sex,' 国家:',$country,'<hr>';

//3. compact() 将变量转换为关联数组
$a = '牛奶';
$b = '果汁';
$c = '茶';
echo var_export(compact('a','b','c')),'<hr>';

//4. explode() 用一个字符分割一个字符串,并将字符串转换为数组
$language = 'html,php,java,python';
//第一个参数为分隔符,以什么符号将其分隔,第二个参数是当前变量
echo var_export(explode(',',$language)),'<br>';
//第三个参数是限制当前数组元素的数量,不丢失数据
echo var_export(explode(',',$language,2)),'<br>';
//删除数组元素 -2
echo var_export(explode(',',$language,-2)),'<br>';
echo '<hr>';
//5. implode() 把数组元素组合为一个字符串
$ary = ['主页','文章','标签','链接'];
echo implode(' ',$ary);
// 变量前可添加分隔符 + , . - =
echo '<hr>';
echo '<a href="#">',implode('</a> <a href="#">',$ary),'</a>';

运行实例 »

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


Correction status:Uncorrected

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