Blogger Information
Blog 48
fans 0
comment 0
visits 36779
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数组与变量和字符串之间的转换
雨天的博客
Original
692 people have browsed it

实例

<?php
//list将数组中的值转为变量(索引数组)
echo '<h3 style="color: #9A0000;">list</h3>';
$stu=['Tom',22,'男'];
list($name,$age,$sex)=$stu;
echo '姓名:'.$name,'--','年龄:'.$age,'--性别:'.$sex,'<br>';

//将数组中的值转为变量(关联数组)
echo '<h3 style="color: #9A0000;">extract()将数组中的值转为变量</h3>';
$stu1=['stuname'=>'小红','score'=>77,'grade'=>3];
var_dump(extract($stu1));//返回数组的长度
echo $stuname.'-'.$score.'-'.$grade.'<br>';

//compact 将变量转为数组
echo '<h3 style="color: #9A0000;">compact()将数变量转为数组</h3>';
$id=22;
$user='会员';
$height=176;
echo '<pre>';
echo var_export(compact('id','user','height'),true).'<br>';

//将字符串转为数组 explode
echo '<h3 style="color: #9A0000;">explode()将字符串转为数组</h3>';
$arr='html,css,php,java';
var_export(explode(',',$arr));
echo '<br>';
var_export(explode(',',$arr,2));//第三个参数代表数组的长度,会把后面的值都放在最后一个里面
var_export(explode(',',$arr,-2));//负数返回除去后面的所有元素的数组

//implode将数组转为字符串
echo '<h3 style="color: #9A0000;">implode()将数组转为字符串</h3>';
$arr1=['java','css','scss','less'];
echo '<br>';
var_export(implode(',',$arr1));
echo '<br>';

运行实例 »

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


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