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
Explain the concept of a PHP session in simple terms.Explain the concept of a PHP session in simple terms.Apr 26, 2025 am 12:09 AM

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

How do you loop through all the values stored in a PHP session?How do you loop through all the values stored in a PHP session?Apr 26, 2025 am 12:06 AM

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

Explain how to use sessions for user authentication.Explain how to use sessions for user authentication.Apr 26, 2025 am 12:04 AM

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

Give an example of how to store a user's name in a PHP session.Give an example of how to store a user's name in a PHP session.Apr 26, 2025 am 12:03 AM

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

What are some common problems that can cause PHP sessions to fail?What are some common problems that can cause PHP sessions to fail?Apr 25, 2025 am 12:16 AM

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

How do you debug session-related issues in PHP?How do you debug session-related issues in PHP?Apr 25, 2025 am 12:12 AM

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

What happens if session_start() is called multiple times?What happens if session_start() is called multiple times?Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

How do you configure the session lifetime in PHP?How do you configure the session lifetime in PHP?Apr 25, 2025 am 12:05 AM

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

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

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor