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 Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

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

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)