search
HomeBackend DevelopmentPHP TutorialPHP Learning Array Courseware Page 1/2_PHP Tutorial

PHP Learning Array Courseware Page 1/2_PHP Tutorial

Jul 21, 2016 pm 03:51 PM
phpsubscriptcode namenameexiststringstudyarrayintegerindexidentify

Subscript: The identification name in the array, which is the code name of the string or integer in the array

The number of index values ​​​​in the array is called a several-dimensional array.
Index value: An index is a structure that sorts the values ​​of one or more columns in a database table.

Array classification
Arrays in PHP are divided into two types:
Indexed array: Indexed (indexed) index value is an integer, starting with 0. Indexed array is used when identifying things by position.
Associative array: Associative (associative) association uses a string as the index value, the index value is the column name, and is used to access the column data.

Arrays are usually assigned by assignment
Generally, there are two ways of assigning arrays:
$a[1]="dsadsadsa";
$b[2]="dsadsadsad" ;
Use the array function:
$a=array("dsads","dsadsa",321312);
One-dimensional array: When there is only one index value (subscript) of the array, it is called one dimensional array.
Format of direct array assignment:
$Array variable name [index value] = data content;
Note: The index value can be a string or an integer, but 1 and "1" are different, they are the same One is an integer and one is a string.

Arrays with the same name without index values ​​are arranged in order.
Example:
$a=array(1,2,3,4,5,6);
$b=array("one", "two", " three");
$c=array(0=>"aaa",1=>"bbb",2=>"ccc");
$d=array("aaa",6 =>"bbb","ccc");
$e=array("name"=>"zhang", "age"=>20);
?>
Two-dimensional Array
Format of multi-dimensional array:
$a[0][]="dsadas";
$a[0][]="dsadsa"; This group is under the 0 index value under $a 1 and 2
If the array function is used to declare the format as follows:
$a=array("dsadsa","dsadas",21,array("dsadsa","dsadas"));

Array traversal
foreach loop structure:
foreach only uses array loops in two formats
foreach(array_exprssion(array expression) as $value);
foreach(array_exprssion(array expression) as $key=>$value);
The first format traverses the given array_exprssion array. Each time through the loop the current value is assigned to my $calue and the pointer inside the array moves forward one step.
The second format does the same thing, except that the key value of the current cell will also be assigned to the variable $key in each loop.
When foreach starts executing, the pointer inside the array will automatically point to the first unit. Also note that foreach operates on a copy of the specified array, not the array itself
$arr=array(10,20,30,40,50,60);
foreach($arr as $k= >$v){
echo "$k=>$v
";
}

Output result: 0=>10 1=>20 2=> 30 3=>40 4=>50 5=>60//Subscript=>integer
Combined use of list(), each() and while loop
each():
$ arr=array(1,2,3,4,5);
$a=each($arr);
print_r($a);
Output result: Array ( [1] => 1 [value] => 1 [0] => 0 [key] => 0 )
Get the first value of the array value subscript key
list():
$arr3 =array("a","b","c");
list($key,$value)=each($arr3);

echo $key."
" .$value;
Output result: 0 a List() can be said to assign a value to a set of variables in one step. It can only be used for numerically indexed arrays and assumes that the numerical index starts from 0.
while loop
$arr=array(1,2,3,4,5,6,7,8,9,);
while(list($key,$value)=each($ arr)){
$key++;
echo $key."=>".$value;
echo "
";
}
echo "
";
Output result: 1=>1 2=>2 3=>3 4=>4 5=>5 6=>6 7=>7 8=>8 9= >9
reset() array pointer redirection
After executing each(), the array pointer will stay at the next unit in the array or at the last unit when the end of the array is reached.
is_array detects whether the variable is an array true and returns true false false
$arr=array(1,2,3,4,5,6,"saas");
while(list($k,$ v) = each($arr))
{
if(is_array($arr))
{
$x += $v;
echo $x;
}
else
{
$x += $k;
}
}
This example cannot fully reflect the function of is_array, but it can be used as a reference.
Array pointer
next(): responsible for moving the pointer backward
prve(): responsible for moving the pointer forward
end(): will point the pointer to the last element of the array
reset(): Unconditionally move the current pointer to the first index position
Syntax format: mixed next (array name)
$arr=(array(1,2,3,4,5));
echo end($arr);
Output result: 5
Key(), current() and count()
The function of key() is to read the index value of the data pointed to by the current pointer.
The current() function reads the content data of the data pointed to by the current pointer.
The count() function is used to count the number of all elements in the array, which means that the function will return the length value of the target array.
Format: int count (array name);

key(): Get the key name from the associative array
$array = array('fruit1' => 'apple','fruit2' => 'orange','fruit3' => 'grape','fruit4' => 'apple','fruit5' => 'apple');
while ($fruit_name = current($array )) {
if ($fruit_name == 'apple') {
echo key($array).'
';
}
next($array);
}
Output results: fruit1, fruit4, fruit5

current(): Return the current unit in the array
$transport = array('foot', 'bike', 'car', 'plane');
$mode = current($transport); // $mode = 'foot';
$mode = next($transport); // $mode = 'bike';
$mode = current($transport); // $mode = 'bike';
$mode = prev($transport); // $mode = 'foot';
$mode = end($transport) ;       // $mode = 'plane';
$mode = current($transport); // $mode = 'plane';
Pay attention to the example to return the current unit in the array

count (): Calculate the number of units in the array
$arr=array(1,2,3,4,5,6);
echo count($arr);
Output result: 6

array_change_key_case()
array_change_key_case returns an array whose string key names are all lowercase or uppercase
The morphological functions included are two [CASE_UPPER] converted to uppercase, [CAS_LOWER] converted to lowercase.
$input_array = array("FirSt" => 1, "SecOnd" => 4);
print_r(array_change_key_case($input_array, CASE_UPPER));
Output result: Array ([FIRST] => 1 [SECOND] => 4 )

array_chunk()
array_chunk() function will decompose the data content of the target array into several small arrays based on the specified number of indexes. in the original array.
$arr=array(1,2,3,4,5,6);
$a=array_chunk($arr,3);
print_r($a);
Output result: Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [1] => Array ( [0] => 4 [1] => 5 [2] => 6 ) )
It is equal to dividing the sum of the number of array units by 3

array_count_values ​​
array_count_values ​​is used to calculate the occurrence of each value in the target array Count
Syntax format: array_count_values ​​(target array)
The result value returned by this function will be expressed in the form of an array using the content data of the original array as an index.
$arr=array(1,2,3,3,2,6);
print_r(array_count_values($arr));
Output result: Array ( [1] => 1 [2 ] => 2 [3] => 2 [6] => 1 )

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319094.htmlTechArticleSubscript: The identification name in the array is also a string or integer. How many codes are there in the array? The index value is called a dimensional array. Index value: The index is a column in the database table or...
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's Purpose: Building Dynamic WebsitesPHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: Handling Databases and Server-Side LogicPHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO)How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python: Code Examples and ComparisonPHP and Python: Code Examples and ComparisonApr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

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: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

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.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software