Table of Contents
'.$tableName.'
Home php教程 php手册 PHP array declaration, traversal, and array global variable usage summary_php basics

PHP array declaration, traversal, and array global variable usage summary_php basics

May 16, 2016 am 09:00 AM

php tutorial: array declaration, traversal, array global variables

<? 
/* 
* 一、数组的概述 
* 1.数组的本质:管理和操作一组变量,成批处理 
* 2.数组时复合类型(可以存储多个) 
* 3.数组中可以存储任意长度的数据,也可以存储任意类型的数据 
* 4.数组可以完成其他语言数据结构的功能(链表,队列,栈,集合类) 
* 
* 
* 
* 二、数组的分类 
* 数组中有多个单元,(单元称为元素) 
* 每个元素(下标[键]和值) 
* 单访问元素的时候,都是通过下标(键)来访问元素 
* 1.一维数组,二维数组,三维数组。。。多维数组 
* (数组的数组,就是在数组中存有其他的数组) 
* 2.PHP中有两种数组 
* 索引数组:就是下标是顺序整数的索引 
* 关联数组:就是下标是字符串作为索引 
* 
* 下标(整数,字符串)只有这两种 
* 
* 
* 三、数组多种声明方式 
* 
* 1.直接为数组元素赋值声明 
* 如果索引下标不给出,就会从0开始顺序索引 
* 如果给出索引下标,下一个就会从最大的开始增1 
* 如果后面出现前面的下标,如果是赋值就是为前面的元素重新赋值 
* 混合声明时,索引和关联不互相影响(不影响索引下标的声明) 
* 
* 2.使用array()函数声明 
* 默认是索引数组 
* 如果为关联数组和索引数组指定下标,使用 键=>值 
* 多个成员之间使用" , "分割 
* 3.使用其他的函数声明 
* 
* 
* 
* 
*/ 
//索引数组 
$user[0]=1;//用户序号 
$user[1]="zhangsan";//用户名 
$user[2]=10;//年龄 
$user[3]="nan";//性别 
echo '<pre class="brush:php;toolbar:false">'; 
print_r($user); 
echo '
Copy after login
';  //关联数组  $user["id"]=1;  $user["name"]="zhangsan";  $user["age"]=10;  $user["sex"];  $user["age"]=90;//赋值  echo $user["name"];//输出  //使用array()声明数组  $user=array(1,"zhangsan",10,"nan");  //使用array()声明关联数组  $user=array("id"=>1,"name"=>"zhangsan","age"=>10,"sex"=>"nan");  //声明多维数组(多条记录),来保存一个表中的多条用户信息记录  $user=array(  //用$user[0]调用这一行,比如调用这条记录中的姓名,$user[0][1]  array(1,"zhangsan",10,"nan"),  //用$user[1]调用这一行,比如调用这条记录中的姓名,$user[1][1]  array(2,"lisi",20,"nv")  );  //数组保存多个表,每个表有多条记录  $info=array(  "user"=>array(  array(1,"zhangsan",10,"nan"),  array(2,"lisi",20,"nv")  ),  "score"=>array(  array(1,90,80,70),  array(2,60,40,70)  )  );  echo $info["score"][1][1];//输出60,  ?>  数组超级全局变量      ";  echo $email."
";  echo $page."
";  //最稳定的取值方法  echo $_GET["username"]."
";  echo $_GET["email"]."
";  echo $_GET["page"]."
";  ?>  this is a $_GET test      username: 
  password: 
        
'; 
print_r($_ENV); 
echo'
Copy after login
';  //显示当前环境  // 也可以单个遍历  ?>  ";  echo $GLOABLS["b"]."
";  echo $GLOABLS["c"]."
";  }  ?>  数组遍历   值变量){  * }  *  *  * 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<4;$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 '';  echo '

'.$tableName.'

';  foreach($table as $row)  {  echo '';  foreach($row as $col)  {  echo ''.$col.'';  }  echo '';  }  echo '';  }  //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);  ?>

the above is a summary of the use of php array declaration, traversal, and array global variables_ php basic content, for more related content, please pay attention to the php chinese website (www.php.cn)!


Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between local variables and global variables of a C++ function? What is the difference between local variables and global variables of a C++ function? Apr 19, 2024 pm 03:42 PM

The difference between C++ local variables and global variables: Visibility: Local variables are limited to the defining function, while global variables are visible throughout the program. Memory allocation: local variables are allocated on the stack, while global variables are allocated in the global data area. Scope: Local variables are within a function, while global variables are throughout the program. Initialization: Local variables are initialized when a function is called, while global variables are initialized when the program starts. Recreation: Local variables are recreated on every function call, while global variables are created only when the program starts.

How to use PHP arrays to generate and display charts and statistical graphs How to use PHP arrays to generate and display charts and statistical graphs Jul 15, 2023 pm 12:24 PM

How to use PHP arrays to generate and display charts and statistical graphs. PHP is a widely used server-side scripting language with powerful data processing and graphic generation capabilities. In web development, we often need to display charts and statistical graphs of data. Through PHP arrays, we can easily implement these functions. This article will introduce how to use PHP arrays to generate and display charts and statistical graphs, and provide relevant code examples. Introducing the necessary library files and style sheets Before starting, we need to introduce some necessary library files into the PHP file

How to use PHP arrays to generate dynamic slideshows and image displays How to use PHP arrays to generate dynamic slideshows and image displays Jul 15, 2023 pm 01:17 PM

How to use PHP arrays to generate dynamic slideshows and picture displays. Slideshows and picture displays are common functions in web design and are often used in scenarios such as carousels and gallery displays. As a popular server-side scripting language, PHP has the ability to process data and generate dynamic HTML pages, and is very suitable for generating dynamic slideshows and picture displays. This article will introduce how to use PHP arrays to generate dynamic slideshows and picture displays, and give corresponding code examples. Prepare image data First, we need to prepare a set of image path data

How to use PHP arrays to implement user login and permission management functions How to use PHP arrays to implement user login and permission management functions Jul 15, 2023 pm 08:55 PM

How to use PHP arrays to implement user login and permission management functions When developing a website, user login and permission management are one of the very important functions. User login allows us to authenticate users and protect the security of the website. Permission management can control users' operating permissions on the website to ensure that users can only access the functions for which they are authorized. In this article, we will introduce how to use PHP arrays to implement user login and permission management functions. We'll use a simple example to demonstrate this process. First we need to create

What are the functions for averaging arrays in php? What are the functions for averaging arrays in php? Jul 17, 2023 pm 04:03 PM

PHP array averaging functions include: 1. array_sum(), which is used to calculate the sum of all values ​​in the array. In order to calculate the average, you can add all the values ​​in the array and then divide by the number of array elements; 2 , array_reduce(), used to iterate the array and calculate each value with an initial value; 3. array_mean(), used to return the average of the array, first calculate the sum of the array, and calculate the number of array elements, then The sum is divided by the number of array elements to get the average.

An exploration of performance optimization techniques for PHP arrays An exploration of performance optimization techniques for PHP arrays Mar 13, 2024 pm 03:03 PM

PHP array is a very common data structure that is often used during the development process. However, as the amount of data increases, array performance can become an issue. This article will explore some performance optimization techniques for PHP arrays and provide specific code examples. 1. Use appropriate data structures In PHP, in addition to ordinary arrays, there are some other data structures, such as SplFixedArray, SplDoublyLinkedList, etc., which may perform better than ordinary arrays in certain situations.

How to convert a two-dimensional php array into a one-dimensional array How to convert a two-dimensional php array into a one-dimensional array Aug 03, 2023 am 11:14 AM

How to convert a php array from two dimensions to a one-dimensional array: 1. Use loop traversal to traverse the two-dimensional array and add each element to the one-dimensional array; 2. Use the "array_merge" function to merge multiple arrays into An array. Pass the two-dimensional array as a parameter to the "array_merge" function to convert it into a one-dimensional array; 3. Using the "array_reduce" function, you can process all the values ​​in the array through a callback function and finally return a result.

How to determine how many arrays there are in php How to determine how many arrays there are in php Aug 04, 2023 pm 05:40 PM

There are several ways to determine an array in PHP: 1. Use the count() function, which is suitable for all types of arrays. However, it should be noted that if the parameter passed in is not an array, the count() function will return 0; 2. Use the sizeof() function, which is more used to maintain compatibility with other programming languages; 3. Custom functions, By using a loop to traverse the array, each time it is traversed, the counter is incremented by 1, and finally the length of the array is obtained. Custom functions can be modified and expanded according to actual needs, making them more flexible.

See all articles