


PHP array declaration, traversal, and array global variable usage summary_php basics
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 '
"; echo $page."
"; //最稳定的取值方法 echo $_GET["username"]."
"; echo $_GET["email"]."
"; echo $_GET["page"]."
"; ?> this is a $_GET test
'; print_r($_ENV); echo'
"; 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 '
'.$col.' | '; } echo '
";//通过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)!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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. 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 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 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

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.

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 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.

PHP array key-value pair is a data structure consisting of a key and a corresponding value. The key is the identifier of the array element, and the value is the data associated with the key. It allows us to store and access data using keys as identifiers. By using key-value pairs, we can more easily operate and manage elements in the array, making program development more flexible and efficient.
