Introduction to Arrays_PHP Tutorial
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"

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
