Blogger Information
Blog 17
fans 0
comment 0
visits 10745
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
变量、数组、字符串相关知识_2018-08-26_17:30提交
斯达融的博客
Original
665 people have browsed it

作业:

实例

<?php
echo "<pre>";
echo "<h3>1、编程:数组与字符串、变量之间的转换</h3><hr>";
echo "<h4>索引数组转变量 list()</h4>";
$namelist=['张三','李四','王五','赵六','周七'];
list($name1,$name2,$name3,$name4,$name5)=$namelist;
echo "name1='{$name1}'<br>name2='{$name2}'<br>name3='{$name3}'<br>name4='{$name4}'<br>name5='{$name5}'<br>";
echo "<h4>关联数组转变量 extract()</h4>";
$user=['name'=>'张三','sex'=>'男','from'=>'中国湖北'];
extract($user);
echo "name='{$name}'<br>sex='{$sex}'<br>from='{$from}'<br>";
echo"<h4>将变量转为关联数组 compact() </h4>";
$name='李四';
$sex='女';
$from='湖北宜昌';
$userinfo=compact('name','sex','from');
echo var_export($userinfo,true);
echo "<h4>将字符串裂变为字符串数组 explode()</h4>";
$namelist="张三,李四,王五,赵六,周七";
$namearray=explode(',',$namelist);
echo   var_export($namearray,true);
echo "<h4>将数组连接为字符串 implode()</h4>";
$namelist=['首页','简介','动态','产品','联系我们'];
echo var_export(implode("|",$namelist),true)."<br>";
echo var_export('<a href="#">'.implode('</a>|<a href="#">',$namelist).'</a>')."<br>";

echo "<hr><h3>2.编程: 数组元素的回调处理 array_filter()  array_walk() </h3>";
$array1 = ['',null,3,6,53,'staroom',true,false];
echo '数组array1: ',var_export($array1,true),',共有:',count($array1),'个元素<br>';
$array2=array_filter($array1);
echo "新数组array2:",var_export(array_filter($array2)),count($array2),'个元素<br>';
$array3=array_filter($array1,function($val){
  return $val >0;
});
echo "数组array3:",var_export($array3,true),count($array3),"个元素<br>";

$keyinfo=[['name'=>'staroom','group'=>'admin'],['name'=>'user1','group'=>'user']];
foreach ($keyinfo as $arr) {
  array_walk($arr,function(&$val,$key){
    echo "{$key} : {$val}<br> ";
  });
  array_walk($arr,function(&$val,$key,$group){
  echo ($val==$group?"passed":"unpassed"),"<br>";
  },'admin');
}
echo "<hr><h3>3.编程: 用for()循环来遍历关联数组</h3><hr>";
$userlist=[];
for($i=0;$i<10;$i++){
  $user=[];
  $user['name']="name{$i}";
  $user['group']="group{$i}";
  $userlist[]=$user;
}
for($i=0;$i<10;$i++){
  echo var_export($userlist[$i],true),"<br>";
  echo "[name]=>",$userlist[$i]['name'],"<br>";
  echo "[group]=>",$userlist[$i]['group'],"<br>";
  echo "<hr>";
}

运行实例 »

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

二、问题: array_walk() 处理数组键值对时,其运行逻辑还未弄清楚,不能达到预期结果。相关测试代码:

实例

<? php
$keyinfo=[['name'=>'staroom','group'=>'admin'],['name'=>'user1','group'=>'user']];
foreach ($keyinfo as $arr) {
  array_walk($arr,function(&$val,$key){
    echo "{$key} : {$val}<br> ";
  });
  array_walk($arr,function(&$val,$key,$group){
  echo ($val==$group?"passed":"unpassed"),"<br>";
  },'admin');
}

运行实例 »

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

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

三、问题解决:经同学指点,已明白array_wlak() 的用法,现练习如下:

实例

<?php
echo "<h3>for循环array_walk() 练习</h3>";
echo "<p><b>题目:</b>有一密闭房间,内有100盏灯,开关编号1-100;有100人,编号1-100;开始房间是全黑的。现在每人做如下操作:将自己编号整数倍的灯的开关按一次,问:最后,房间里有哪些灯是亮着的?</p>";

echo "<hr>";

$lamplist=[];
$personlist=[];

for($i=0;$i<100;$i++){
$lamplist[$i+1]='off';
$personlist[$i+1]='undo';
}

//开始
 for($i=1;$i<101;$i++){
   for($k=1;$k<101;$k++){
     if($k%$i==0){
       $lamplist[$k]= $lamplist[$k]=='on'?'off':'on';
     }
   }
   $personlist[$i]='do';
 }

echo "最后还亮着的灯有:<br>";

array_walk($lamplist,function(&$value,$key,$state){
  echo $value==$state?$key."号灯<br>":"";
},'on');

运行实例 »

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


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