search
HomeBackend DevelopmentPHP TutorialIntroduction to Arrays_PHP Tutorial
Introduction to Arrays_PHP TutorialJul 13, 2016 pm 05:45 PM
phpintroducelandfastsupplydataarraymethodyesOverviewofRelatedprogrammingmanage

Array

1. Overview

Arrays provide a quick and convenient way to manage a group of related data and are an important part of PHP programming. Through arrays, a large amount of data of the same nature can be stored, sorted, inserted and deleted, etc., which can effectively improve the efficiency of program development and improve the way programs are written.

An array is a collection of data. The data is organized according to certain rules to form an operable whole. It is one of the means to effectively organize and manage a large amount of data. A large amount of data of the same nature can be stored through the array function. Sorting, inserting and deleting operations can effectively improve program development efficiency and improve the way programs are written.

2.Array type

The essence of an array is to store, manage and operate a set of variables. In PHP, arrays are divided into one-dimensional arrays, two-dimensional arrays and multi-dimensional arrays. However, whether it is one-dimensional or multi-dimensional, arrays can be uniformly divided into two types: numerical index arrays and associative arrays. Numeric index arrays use numbers as key names, and association arrays use numbers as key names. Arrays use strings as keys.


Numeric index array, the subscript (key name) consists of numbers, starting from 0 by default. Each number corresponds to the position of the array element in the array. There is no need to specify it. PHP will automatically assign an integer value to the key name of the numerical index array. Then it automatically increments from this value, or you can specify the position to start from.

Associative array, the subscript (key name) is an array composed of a mixture of numeric values ​​and strings.

Arrays require key names to access the values ​​stored in the array.


3. Declare array
Note: 1. The array name starts with $, the first character is a letter or underscore, followed by any number of letters, numbers or underscores.
2. In the same program, scalar variables and array variables cannot have the same name.
3. The name of the array is case-sensitive

There are two ways to declare an array, namely user declaration and function declaration.

1. User Statement
$array['0']="php array";
$array['1']="php learning";

2. Function declaration
$arrr=array('one'='php','two'='java');

Create a two-dimensional array. What is created above has only one column of data content, so it is called a one-dimensional array. If two one-dimensional arrays are combined into one array, it is a two-dimensional array.
Example: $str=array(
"Network"=>array("ip address","gateway");
"Book"=>array("Spring and Autumn","Warring States");
)


4. Traverse the array
Traversing an array means accessing each element in the array in a certain order until all elements are accessed. In PHP, arrays can be traversed through process statements (foreach and for loop statements) and functions (list() and each()).

foreach(),
To traverse an array, you need to use the count() function to get the number. If the array to be traversed is a numeric index array, and the index value of the array is a continuous integer, you can use a for loop to traverse, but the prerequisite is that you need to apply the count() function to obtain the number of elements in the array, and then use the obtained The number of elements is used as the execution condition of the for loop to complete the array loop.
Example: for($i=0;$i"}

Traverse the array through the array functions list and each:

The list() function assigns the values ​​in the array to some variables. This function can only be used for numerically indexed arrays, and the numerical index starts from 0. Syntax: void list(mixed...) The parameter mixed is the name of the variable to be assigned.
The each() function returns the key name and corresponding value in the array, and moves the array pointer forward. Syntax: array each (array array) The parameter array is the input array.
Example: Use the list function to obtain the value of the array returned in the each function, and assign it to $name and $value respectively, and then use a while loop to output
while(list($name,$value)=each($array)){echo $name=$value}
5. Output array elements
The print_r() function can output the structure of the array, or the var_dump() function can be used to output the array structure.
The echo statement simply outputs an element in the array, and must have an identifier and an array index in the format echo $array[0]. Similarly, the print statement can also output an element in the array.

6.php array function
Array functions that are commonly used in development.

The count() function can count the number of elements in the array
The array_push() function adds elements to an array. Appends the passed element to the end of an array and returns the total number of new elements in the array. Syntax: array_push($array,"New Element 1","New Element 2")
The array_pop function can get and return the last element in the array, and reduce the length of the array by 1. If the array is empty (or not an array), it will return null

Remove duplicate elements from an array:
The array_unique() function can delete duplicate elements in the array
Use unset() to delete an element in an array. unset($arr_int[1]);

Get the key name of the specified element in the array: Obtaining the key name of the specified element in the array is mainly achieved through array functions.

array_search() is used to obtain the key name of the specified element in the array, search for the given value in the array, and return the key name after finding it, otherwise return FALSE, distinguishing between upper and lower case letters
array_search(orange, $arr[is case sensitive])

Use the array_keys() function to get all the key names of repeated elements in the array

The conversion of strings and arrays is essential and is mainly implemented using the explode and implode functions.
explode
Implode converts array elements into string type

7.php global array

Using the PHP global array, you can obtain a large amount of environment-related information, such as the current user session, user operating environment, local operating environment, etc.

$_SERVER[] global array to obtain information about server and client configuration and current request
The $_GET[] and $POST[] global arrays are used to receive the data passed to the current page by the GET and POST methods respectively.
$_COOKIE[] global array stores information passed to the script through http cookies
$_ENV[] is used to provide server-related information
$_REQUEST[] can be used to obtain the information passed to the script by GET, POST and http cookies.
$_SESSION[] is used to obtain information about session variables
$_FILES[] is a special array, which will be explained in detail later


Review:

1.Array overview
2. Array type 2 types
3. Declare array User creation and function creation, one-dimensional array, two-dimensional array
4. Traverse and output arrays foreach and for and the functions list() and each() to traverse multi-dimensional arrays. The output includes var_dump, print_r, echo $ss[1]
5.php array function counts array elements, adds elements to the array, gets the last element in the array, deletes duplicate elements in the array, and gets the key name of the specified element in the array
6.php global arrays Which ones are commonly used

Author "Technology is King"

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478634.htmlTechArticleArray 1. Overview Arrays provide a quick and convenient way to manage a group of related data and are php programs important content in design. Arrays can be used to process a large amount of data of the same nature...
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()”在数组中查找一个键值。

go语言中元组是什么go语言中元组是什么Dec 27, 2022 am 11:27 AM

元组是固定长度不可变的顺序容器(元素序列),go语言中没有元组类型,数组就相当于元组。在go语言中,数组是一个由固定长度的特定类型元素组成的序列,一个数组可以由零个或多个元素组成;数组的声明语法为“var 数组变量名 [元素数量]Type”。

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

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

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尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools