Blogger Information
Blog 11
fans 0
comment 1
visits 15621
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP数组使用2
JasonKim的博客
Original
1156 people have browsed it
1、list($var,$var1); 将数组转换成变量
list($name,$course,$grade) = ['小龙女','PHP',99];
echo $name.'的',$course.'课程的成绩是',$grade.'分';

2、extract($arr,$flag); 将关联数组转换成变量
$arr1 = ['id'=>10,'name'=>'杨过','sex'=>'male','salary'=>8900];
echo '共生成了:',var_export(extract($arr1),true),'个变量<hr>';
echo '我的ID是'.$id,'姓名是:'.$name,'性别:'.$sex,'工资'.$salary,'元<hr>';

3、compact($arr,$arr1); 将变量转换成数组
$name = '陈近南';
$faction = '天地会';
$position = '总舵主';
$age = 28;
// 参数与变量名称对应
$arr2 = compact('name','faction','position','age');

4、explode(); 将字符串转换为数组
$lang = 'html,css,jq,php,mysql,redis';
echo var_export(explode(',',$lang),true);
// 限制转换数组元素的数量
echo var_export(explode(',',$lang,4),true);
// limit 为负数,负数就是要删除的元素个数,从末尾删除两个
echo var_export(explode(',',$lang,-2),true);

5、implode($glue,$arr); 将数组转换成字符串
$arr = ['首页','公司新闻','公司产品','联系我们'];
echo var_export(implode('|',$arr),true);
// 实现 <a> 标签,变成导航
$str = '<a href="#">'.implode('</a> | <a href="#">',$arr).'</a>';
echo var_export($str,true);


//使用implode() 将关联数组转换成sql语句
// 'INSERT INTO 表名 (字段列表) VALUES (值列表)';
//1、要添加的记录,以关联数组的方式
$staff = ['name'=>'Jason','sex'=>0,'age'=>22,'salary'=>9800];
// 2、生成SQL语句
$sql = "INSERT INTO `staff` ":
// 3、生成SQL语句中的字段列表    array_keys: 获取数组键名
$fields = "`".implode("`,`",array_keys($staff))."`";
// 4、拼接SQL
$sql .= " (".$fields.") ";
// 5、生成sql语句的列表值
$value = " VALUES ('".implode("','",$staff)."'); ";
// 6、查看生成是SQL语句
echo $sql;


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