search
HomeBackend DevelopmentPHP TutorialIntroduction to PHP Arrays_PHP Tutorial

Introduction to PHP arrays

Array basics:
In php, the subscript of an array can be an integer or a string
In php, the order of the elements of the array is not determined by the subscript, but by the order of their "joining"
Definition:
$arr1 = array(element 1, element 2,...);
array(1,1.1,5,'abc',true,false); //Any data can be stored, at this time it is the default subscript
array(2=>1,4=>1.1,3=>5,7=>'abc',0=>true); //The subscripts can be set arbitrarily (no order required, No need to be consecutive)
array(2=>1,1.1,1=>5,'abc',0=>true); //You can add a subscript or not. If you don't add a subscript, it will be the default subscript. Mark
                                                                                                                                                                            to
// The number of this number is: 2, 3, 1, 4, 0
array(2=>1,'dd'=>5,1=>1.1,'abc',0=>true); //Mixed subscripts, also follow the default subscript rules
array(-2=>1,'dd'=>5,1.1,'abc',true); //Negative subscripts are not included in the integer subscripts, but are only treated as character subscripts
// The last three sets of this array are: 0,1,2
array(2.7=>1,'dd'=>5,1=>1.1,'abc',true); //The floating point number subscript will be automatically converted to an integer, and the decimal part will be removed directly
array("2.7"=>1,'dd'=>5,"11"=>1.1,'abc',true); //Pure numeric string subscript, treated as a number
array(2=>1,'dd'=>5,true=>1.1,'abc',false=>true); //Boolean value is used as subscript, then true is 1, false is 0
array(2=>1,'dd'=>5,2=>1.1,'abc',true); //If the subscript is repeated with the previous one, it will simply overwrite the value of the previous subscript with the same name.
Other forms:
$arr1[] = 1;
$arr1[] = 5;
$arr1[] = 1.1;
       … //Use [] directly after the variable to become an array, and assign values ​​​​in sequence
$arr2['aa'] = 1;
$arr2['bb'] = 5;
$arr2[5] = 1.1;
     …                                                                                                                                                    //The subscript written in this form is actually almost the same as using the array syntax structure
Classification of arrays:
From the key-value relationship, it is divided into:
Associative array: usually refers to an array whose subscript is a string, and the string can roughly express the meaning of the data
              Example: $person = array("name" => "poe", "age" => 18, "edu" => "University graduate");
Index array: usually refers to an array whose subscript strictly starts from 0 and is a continuous numeric subscript - similar to js array
Divided from the array level:
One-dimensional array: Each element value in an array is an ordinary value (non-array value)
Example: $ Person = Array ("name" = & gt; "poe", "age" = & gt; 18, "edu" = & gt; "college graduation");
Two-dimensional array: Each item in an array is also a one-dimensional array.
$person = array(
                                             "name" => array("xiaohua","xiaofang),
                                                                                                                                                                                                                  "age" => array(18,22),
"Edu" = & GT; Array ("University Graduation", "Primary School",)
                      );
Multi-dimensional array: and so on. . .
The general syntax form of multi-dimensional arrays:
$ v1 = number of array names [Nubble] [Laying] [......]
Array traversal:
Basic syntax for traversal:
foreach($arr as [$key =>] $value) {
                //Here you can perform all possible operations on $key and $value -- because they are just a variable
//$key represents the subscript of each element obtained, which may be a number or a string
//$value represents the value of the element obtained each time, which may be of various types
                             //This loop structure will traverse from the first item of the array to the last item, and then end
}
Array pointers and traversal principles:
Each array has a "pointer" inside it, which determines the element obtained when the array currently takes value
During the foreach traversal process, it all relies on this pointer.
Example: $arr1 = array(2=>1,'dd'=>5,1=>1.1,'abc',0=>true);
wKioL1ZNx1Sxflt6AAAnrLtN90Q498.jpg
In addition to the position setting of the foreach loop, pointers also have some other functions that rely on pointers:
1: $v1 = current($arr1); //Get the value of the element pointed to by the current pointer in $arr1. If there is no element pointed to, it is false
2: $v1 = key($arr1); //Get the subscript of the element pointed to by the current pointer in $arr1. . . . . . . . . . . . .
3: $v1 = next($arr1); //Move the pointer to the "next element", and then get the value of the next element
4: $v1 = prev($arr1); //Move the pointer to the "previous element", and then get the value of the previous element
5: $v1 = reset($arr1); //Move the pointer to the "first element" and get the value of the element
6: $v1 = end($arr1); //Move the pointer to the "last element" and get the value of the element
7: $v1 = each($arr1); //Get the subscript and value of the current element, and then move the pointer to the next position

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1073352.htmlTechArticlePHP array introduces the basics of arrays: In php, the subscript of an array can be an integer or a string. In php, The order of the elements of the array is not determined by the subscript, but by the order in which they are added...
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
php 怎么求2个数组相同的元素php 怎么求2个数组相同的元素Dec 23, 2022 am 10:04 AM

php求2个数组相同元素的方法:1、创建一个php示例文件;2、定义两个有相同元素的数组;3、使用“array_intersect($array1,$array2)”或“array_intersect_assoc()”方法获取两个数组相同元素即可。

c语言数组如何初始化c语言数组如何初始化Jan 04, 2023 pm 03:36 PM

C语言数组初始化的三种方式:1、在定义时直接赋值,语法“数据类型 arrayName[index] = {值};”;2、利用for循环初始化,语法“for (int i=0;i<3;i++) {arr[i] = i;}”;3、使用memset()函数初始化,语法“memset(arr, 0, sizeof(int) * 3)”。

用Python实现动态数组:从入门到精通用Python实现动态数组:从入门到精通Apr 21, 2023 pm 12:04 PM

Part1聊聊Python序列类型的本质在本博客中,我们来聊聊探讨Python的各种“序列”类,内置的三大常用数据结构——列表类(list)、元组类(tuple)和字符串类(str)的本质。不知道你发现没有,这些类都有一个很明显的共性,都可以用来保存多个数据元素,最主要的功能是:每个类都支持下标(索引)访问该序列的元素,比如使用语法Seq[i]​。其实上面每个类都是使用数组这种简单的数据结构表示。但是熟悉Python的读者可能知道这3种数据结构又有一些不同:比如元组和字符串是不能修改的,列表可以

c++数组怎么初始化c++数组怎么初始化Oct 15, 2021 pm 02:09 PM

c++初始化数组的方法:1、先定义数组再给数组赋值,语法“数据类型 数组名[length];数组名[下标]=值;”;2、定义数组时初始化数组,语法“数据类型 数组名[length]=[值列表]”。

javascript怎么给数组中增加元素javascript怎么给数组中增加元素Nov 04, 2021 pm 12:07 PM

增加元素的方法:1、使用unshift()函数在数组开头插入元素;2、使用push()函数在数组末尾插入元素;3、使用concat()函数在数组末尾插入元素;4、使用splice()函数根据数组下标,在任意位置添加元素。

php怎么判断数组里面是否存在某元素php怎么判断数组里面是否存在某元素Dec 26, 2022 am 09:33 AM

php判断数组里面是否存在某元素的方法:1、通过“in_array”函数在数组中搜索给定的值;2、使用“array_key_exists()”函数判断某个数组中是否存在指定的key;3、使用“array_search()”在数组中查找一个键值。

php 怎么去除第一个数组元素php 怎么去除第一个数组元素Dec 23, 2022 am 10:38 AM

php去除第一个数组元素的方法:1、新建一个php文件,并创建一个数组;2、使用“array_shift”方法删除数组首个元素;3、通过“print_”r输出数组即可。

详解Go语言中删除数组元素的方法详解Go语言中删除数组元素的方法Mar 22, 2023 pm 03:21 PM

在Go语言中,数组是一种重要的数据类型。它与其他语言的数组一样,是一组相同类型的数据组成,可以通过一个索引来访问数组中的元素。在某些情况下,我们需要从一个数组中删除元素,本文将会介绍在Go语言中如何实现数组删除。

See all articles

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools