Home >php教程 >php手册 >php数组声明,遍历,数组全局变量

php数组声明,遍历,数组全局变量

WBOY
WBOYOriginal
2016-06-06 19:56:351278browse

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 foreach($user as $key=$val)//$val $key 都是自定义变量 { echo $key."=====".$val."br"; } //foreach遍历多维数组 $info=array( "user"=array( //$user[0] array(1, "zansan", 10, "nan"), //$user

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

 

  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 '

';

  echo '

';

  foreach($table as $row)

  {

  echo '

';

  foreach($row as $col)

  {

  echo '

';

  }

  echo '

';

  }

  echo '

'.$tableName.'

'.$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);

  ?>

  [1] [2] [3] 

php数组声明,遍历,数组全局变量

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn