Blogger Information
Blog 12
fans 0
comment 0
visits 10336
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php中数组与字符串及变量之间的转换
阿杜的博客
Original
1446 people have browsed it

php中数组与字符串及变量之间的转换函数

<?php

//数组分类 索引数组 关联数组

//索引数组 索引从0开始计数

$sarr = ['1bb','2bb','3bb','4bb','5bb'];

// echo $sarr[0].'<br>';

//关联数组  键名是字符串

$garr = ['name'=>'adu','school'=>'xinkaisi'];

echo $garr['school'].'<br>';

//数组不能进行字符串转换

for ($i=0; $i<count($sarr); $i++)

{

$res .= $sarr[$i];

}

echo $res;

// $a='';

for ($i=0; $i<count($sarr); $i++)

{

// echo $sarr[$i].'<br>';

$a .= $i<count($sarr) ? $sarr[$i].',' : $sarr[$i];


}

echo $a;

echo '<hr>explode()将字符串转换成数组<br>';

$lang = 'html,css,javascript,php,java,mysql';

echo var_export(explode(',',$lang)).'<br>';


echo '<hr>list(): 索引数组转换为变量<br>';

$larr = [6,7,8,9,10];

list($a6,$a7,$a8,$a9,$a10)=$larr;

echo '$a6=', $a6,'$a7=', $a7;


echo '<hr>extract(): 关联数组转为变量,返回值是变量的数量<br>';

$arr=['name'=>'adu','faction'=>'天地会','position'=>'总舵主'];

extract($arr,true);

echo '共生成了:',var_export(extract($arr),true),'个变量:<br>';

echo '$name='.$name.' $faction='.$faction.' $position='.$position;


echo '<hr>compact(): 将变量转换为关联数组<br>';

echo '$var1 = \'var1\'<br>';

echo '$var2 = \'var2\'<br>';

echo '$var3 = \'var3\'<br>';

$var1 = 'var1';

$var2 = 'var2';

$var3 = 'var3';

$arr2 = compact('var1','var2','var3');

echo var_export($arr2);


echo '<hr>implode(): 数组元素拼接成一个字符串<br>';

$imarr = ['首页','新闻','下载','联系'];

echo var_export(implode($imarr),true),'<br>';

echo var_export(implode('||',$imarr),true),'<br>';

//添加<a>转为导航

echo var_export("<a href='#'>".implode('</a>|<a href="#">',$imarr).'</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