复制代码 代码如下:
/* 数组的遍历
*
* 1.使用for语句循环遍历数组
* 1.其他语言(只有这一种方式)
* 2.PHP中这种方式不是首选的方式
* 3.数组必须是索引数组,而且下标必须是连续的。
* (索引数组下标可以不连续,数组还有关联数组,这两种不能遍历)
*
* 2.使用foreach语句循环遍历数组
* foreacho(数组变量 as 变量值){
* //循环体
* }
* 1.循环次数由数组的元素个数决定
* 2.每一次循环都会将数组中的元素分别赋值给后面变量
*
* foreach(数组变量 as 下标变量=> 值变量){
* }
*
*
* 3.while() list() each() 组合循环遍历数组
*
* each()函数:
* 1.需要一个数组作为参数
* 2.返回来的也是一个数组
* 3.返回来的数组是0,1,key,value四个下标(固定的)
* 0和key下标是当前参数数组元素的键
* 1和value下标是当前参数数组元素的值
* 4.默认当前元素就是第一个元素
* 5.每执行一次后就会将当前元素向后移动
* 6.如果到最后的元素再执行这个函数,则返回false
* list()函数:
* 1. list()=array();需要将一个数组赋值给这个函数
* 2.数组中的元素个数,要和list()函数中的参数个数相同
* 3.数组中的每个元素值会赋值list()函数中的每个参数,list()将每个参数转为变量
* 4.list()只能接受索引数组
* 5.按索引的下标顺序来给参数赋值
*
*
*
*/
//for语句遍历数组
$user=array(1,"zhangsan",40,"nan");
for($i=0;$i{
echo"\$user[{$i}]=".$user[$i]."
";
}
//使用foreach
$user=array(1,"zhangsan",40,"nan");
foreach($user as $val)//$val是自定义变量
{
echo $val."
";//输出与下标无关
}
foreach($user as $key=>$val)//$val $key 都是自定义变量
{
echo $key."=====>".$val."
";
}
//foreach遍历多维数组
$info=array(
"user"=>array(
//$user[0]
array(1, "zansan", 10, "nan"),
//$user[1][1]
array(2, "lisi", 20, "nv"), //$user[1]
//$user[2]
array(3, "wangwu", 30, "nan")
),
"score"=>array(
array(1, 100, 90, 80),
array(2, 99, 88, 11),
array(3, 10, 50, 88)
),
"connect"=>array(
array(1, '110', 'aaa@bbb.com'),
array(2, '120', 'bbb@ccc.com'),
array(3, '119', 'ccc@ddd.com')
)
);
foreach($info as $tableName=>$table)
{
echo '
'.$col.' | ';
}
//each()的使用
$user=array("id"=>1,"name"=>"zhangsan","age"=>10,"sex"=>"nan");
$a=each($user);//Array ( [1] => 1 [value] => 1 [0] => id [key] => id ) 默认是第一个元素的值
print_r($a);
$b=each($user);
print_r($b);//Array ( [1] => zhangsan [value] => zhangsan [0] => name [key] => name ) 每执行一次,向后遍历一个
$c=each($user);
print_r($c);//Array ( [1] => 10 [value] => 10 [0] => age [key] => age )
$d=each($user);
print_r($d);//Array ( [1] => nan [value] => nan [0] => sex [key] => sex )
$e=each($user);
var_dump($e);//bool(false) 当没有元素时,返回的值
//each()配合while遍历
$user=array("id"=>1,"name"=>"zhangsan","age"=>10,"sex"=>"nan");
while($arr=each($user))
{
//echo $arr[0]."====>".$arr[1]."
";//通过0,1 来显示 键(下标) 和 值
echo $arr["key"]."===>".$arr["value"]."
";//通过key,value 来显示 键 值
}
//list()函数的使用
list($name,$age,$sex)=array("zhangsan",10,"nnnnn");
echo $name."
";
echo $age."
";
echo $sex."
";
//另一种使用方法
list(,,$sex)=array("zhangsan",10,"nnnnn");
echo $sex."
";//只把性别转换为变量
//ip判断
$ip="192.168.1.128";
list(,,,$d)=explode(".",$ip);//explode表示用 . 来分隔,并返回一个数组
echo $d;//取出128
//list()只能接收索引数组的例子
$user=array("id"=>1,"name"=>"zhangsan","age"=>10,"sex"=>"nan");
list($key,$value)=each($user);//Array( [1]=>1 [0]=>id) 按照索引下标的顺序给list中的参数赋值,所以先是 0键 然后是 1值
echo $key."--->".$value;
//while list() each() 组合使用
$user=array("id"=>1,"name"=>"zhangsan","age"=>10,"sex"=>"nan");
while(list($key,$value)=each($user))
{
echo $key."--->".$value."
";
}
//多次循环只显示一次的解决方法
//使用数组的内部指针控制函数
//next(数组);数组指针移动到下一个
//prev(数组);数组指针移动到上一个
//reset(数组);数组指针移动到第一个(复位)
//end(数组);数组指针移动到最后一个
//current(数组);获取当前元素的值,当前元素时指数组指针指向的元素。
//key(数组);获取当前元素的键值(下标)
$user=array("id"=>1,"name"=>"zhangsan","age"=>10,"sex"=>"nan");
while(list($key,$value)=each($user))
{
echo $key."--->".$value."
";
}
//在这里将数组指针移动到第一个以下循环就能输出
//reset($user)
while(list($key,$value)=each($user))//因为each()到最后一个返回false,所以循环直接跳出
{
echo $key."--->".$value."
";
}
while(list($key,$value)=each($user))//因为each()到最后一个返回false,所以循环直接跳出
{
echo $key."--->".$value."
";
}
echo current($user)."=====>".key($user);
?>

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor
