001——数组(一)数组知识及foreach函数应用
<?php /**数组(一)数组知识及foreach函数应用*/ /*数组:在一个变量中,存储一个或多个值,每一个元素都有一个访问ID * * */ /* * //索引型数组 $arr=array('张三','PHP视频课程','29','1988-05-06'); print_r($arr);//输出:Array ( [0] => 张三 [1] => PHP视频课程 [2] => 29 [3] => 1988-05-06 ) echo "<br/>学员名是:".$arr[0]."所学课程:".$arr[1]."年龄是:".$arr[2]."生日是:".$arr[3]; //输出:学员名是:张三所学课程:PHP视频课程年龄是:29生日是:1988-05-06 */ //关联型数组 /* $arr1=array("uname"=>"李四",'uclass'=>"DIV+CSS","age"=>14,"birth"=>"1989-07-06"); print_r($arr1);//输出:Array ( [uname] => 李四 [uclass] => DIV+CSS [age] => 14 [birth] => 1989-07-06 ) echo "<br/>学员名是:".$arr1["uname"]."所学课程:".$arr1['uclass']."年龄是:".$arr1["age"]."生日是:".$arr1["birth"]; //输出:学员名是:李四所学课程:DIV+CSS年龄是:14生日是:1989-07-06 */ //多维数组的定义: /* $arr2=array(array("uname"=>"王五","age"=>14),array("uname"=>"张三","age"=>14)); echo $arr2[1]['uname'];//输出:张三 */ /* $arr3[0]="蓝天"; $arr3[1]="白云"; $arr3[3]="流水"; print_r($arr3);//输出:Array ( [0] => 蓝天 [1] => 白云 [3] => 流水 ) */ /* $arr4[]="PHP课程"; $arr4[]="JAVA课程"; $arr4[]="DIV+CSS课程"; $arr4[]="HTML5课程"; print_r($arr4);//输出:Array ( [0] => PHP课程 [1] => JAVA课程 [2] => DIV+CSS课程 [3] => HTML5课程 ) */ //定义一个多维数组: //foreach循环:对一维数组的操作: /*$arr3[0]="蓝天"; $arr3[1]="白云"; $arr3[2]="流水"; foreach ($arr3 as $k=>$value){ echo "<span style='color: red;border:1px #188eee solid'>".$value."</span>"; echo $k; }*/ $arr5["PHP"] = array(); $arr5['PHP']['cname'] = "php课程"; $arr5['PHP']['ctime'] = "300课时"; $arr5['PHP'][] = "数据处理"; $arr5['PHP'][] = "模板引擎"; $arr5['DIV'][] = "HTML基础知识"; $arr5['DIV'][] = "CSS布局"; $arr5['DIV'][] = "整站开发处理"; $arr5['HTML'][] = "HTML基础知识"; $arr5['JS'][] = "JS基础知识"; /*foreach ($arr5 as $key=>$value){ echo "<div style='border: 1px #000 solid; margin-bottom: 10px;'>"; if(is_array($value)){ foreach ($value as $k=>$v){ echo " ".$k."=>"."$v"; } }else{ echo $key."=>".$value; } echo "</div>"; }*/ $db = new mysqli('localhost', 'root', '123456', 'edu') or die('连接错误'); $db->query('set names gbk'); $sql = "select * from user"; $result = $db->query($sql); //fetch_array()如果单独作为while的条件,则能够取出结果集中的所有结果。如果单独作用于结果集,只能从结果集中取出数据库结果集一行。 while ($row = $result->fetch_assoc()) { print_r($row); }