Home Backend Development PHP Tutorial PHP learning officially sets sail (3)

PHP learning officially sets sail (3)

Dec 28, 2016 am 09:12 AM
php

Now talk about arrays
There are 3 types of php arrays

Numeric array Array with numeric ID key
Associative array Each ID key in the array is associated with a value
Multidimensional array Array containing one or more arrays

Declaration about arrays

<?php
$a[5];
$a[5]={1,2,3,4,5};
?>
Copy after login

Conventional 2 types like C language will not work in PHP

php has the keyword array which is used to define arrays

<?php
$a=array();
?>
Copy after login

This defines an empty array. There is no need to specify the length. The elements inside can be added dynamically. How many are added? This The array is as big as it is, and you can continue to add it. This is very cool.

<?php
$a=array();
echo $a;
?>
Copy after login

This way you can print the type of a and the result is Array

But in this case, an error will be reported

<?php
$a=array();
echo $a[0];
?>
Copy after login

Because the array is empty

There are two main ways to define an array. One is to use the array

<?php
$a=array(1,2,3,4,5);
for($i=0;$i<count($a);$i++)
echo $a[$i]."<br/>";
?>
Copy after login

count keyword to calculate how many there are in an array. elements

Another option is to

<?php
$a[0]=&#39;a&#39;;
$a[1]=&#39;b&#39;;
$a[2]=&#39;c&#39;;
for($i=0;$i<count($a);$i++)
echo $a[$i]."<br/>";
?>
Copy after login

directly assign a value to the variable subscript, and the variable will automatically become an array

But the values ​​must be assigned in subscript order
For example, this is wrong

<?php
$a[0]=&#39;a&#39;;
$a[1]=&#39;b&#39;;
$a[2]=&#39;c&#39;;
$a[5]=&#39;d&#39;;
for($i=0;$i<count($a);$i++)
echo $a[$i]."<br/>";
?>
Copy after login

Also the elements in the array can not be of the same type

<?php
$a=array(1,&#39;b&#39;,"hello",1.0);
for($i=0;$i<count($a);$i++)
echo $a[$i]."<br/>";
?>
Copy after login

Do you think it is very powerful


Now let’s talk about associative arrays
Associative arrays are more powerful than ordinary arrays. The subscripts do not need to use numbers, but choose their own names.
This is a key-to-value relationship, similar to java’s map* *Very similar

<?php
$a=array("a"=>1,&#39;b&#39;=>2,"c"=>3);
echo $a["a"]."<br/>";
echo $a[&#39;b&#39;]."<br/>";
echo $a["c"]."<br/>";
?>
Copy after login

Note that it is=>not->, the single quotes and double quotes inside can be interchanged
Key=>The value key can be repeated, but the result is to display the last one

<?php
$a=array("a"=>1,&#39;a&#39;=>2,"c"=>3);
echo $a["a"]."<br/>";
echo $a["c"]."<br/>";
?>
Copy after login

Print 2 3

There is also a definition of associative array, which is the one mentioned above

<?php
$a["a"]="hello";
$a["b"]="world";
echo $a["a"]."<br/>";
echo $a["b"]."<br/>";
?>
Copy after login

But the following is wrong

<?php
$a["a"]=>"hello";
$a["b"]=>"world";
echo $a["a"]."<br/>";
echo $a["b"]."<br/>";
?>
Copy after login

In addition, numbers can also be used as keys

<?php
$a["1"]="hello";
$a["2"]="world";
echo $a["1"]."<br/>";
echo $a["2"]."<br/>";
?>
Copy after login

is feasible

It can be output without quotation marks, but php has a prompt to note, do not use this

<?php
$a[&#39;a&#39;]="hello";
$a[&#39;b&#39;]="world";
echo $a[a]."<br/>";
echo $a [ b ]";
?>
Copy after login

Let’s talk about multi-dimensional arrays
In multi-dimensional arrays, each element in the main array is also an array. Each element in the sub-array can also be an array, and so on

This defines a multi-dimensional array, two-dimensional

<?php
$a=array(array(&#39;a&#39;,1,2),array("hello",3,1.1,));
echo $a[0][0];
?>
Copy after login

In C language, it is a [2][3] It doesn’t matter how many dimensions the array has.
Similarly, the elements in a multi-dimensional array can also be of multiple types
And it can also be like this

<?php
$a=array(array(&#39;a&#39;,1,2),array("hello",3,1.1,2,&#39;a&#39;));
echo $a[0][0];
?>
Copy after login

Not required The number of elements in each sub-array is the same, which is better than C language.

Like ordinary arrays, multi-dimensional arrays can also be defined in this way, but I don’t think anyone will do this. .

<?php
$a[0][0]="hello00";
$a[0][1]="hello01";
$a[0][2]="hello02";
$a[0][3]="hello03";
$a[1][0]="hello10";
$a[1][1]="hello11";
$a[1][2]="hello12";
$a[1][3]="hello13"; 
for($i=0;$i<2;$i++){
for($j=0;$j<4;$j++)
echo $a[$i][$j]." "; echo "<br/>";
}
?>
Copy after login

This two-dimensional array is a[2][4] with 2 rows and 4 columns. It is relatively regular.
Note that only the number of elements in the columns can be printed using a loop. In C language, You don’t need to consider this sentence

You can also define a multi-dimensional associative array

<?php
$a=array(&#39;a&#39;=>array(&#39;a&#39;=>"hello",&#39;b&#39;=>"world"),&#39;b&#39;=>array(&#39;one&#39;=>1,&#39;two&#39;=>2,&#39;three&#39;=>3)); 
echo $a[&#39;a&#39;][&#39;a&#39;]; 
?>
Copy after login

will print hello
, which looks a bit dizzy, because the associative array contains The associated
does not need to be like this. Like the following, it will be clear that many

<?php
$a=array(&#39;a&#39;=>array("hello","world"),&#39;b&#39;=>array(1,2,3)); 
echo $a[&#39;a&#39;][0]."<br/>".$a[&#39;b&#39;][2]; 
?>
Copy after login

print out

hello
3
Copy after login

must not be played like this

<?php
$a=array(array(&#39;a&#39;=>"hello",&#39;b&#39;=>"world"),array(&#39;one&#39;=>1,&#39;two&#39;=>2,&#39;three&#39;=>3)); echo $a[&#39;a&#39;]; ?>
Copy after login

Wrong



What I mentioned above are all two-dimensional arrays, so how to define three-dimensional or above arrays is very simple

<?php
$a=array(array(array(1,2,3))); 
echo $a[0][0][0]; 
?>
Copy after login

Print 1

<?php
$a=array(array(array(1,2,3)),array(array(4,5,6))); 
echo $a[1][0][0]; 
?>
Copy after login

Print 4

<?php
$a=array(array(array(1,2,3),array(4,5,6)),array(array(7,8,9))); 
echo $a[0][1][1]; 
?>
Copy after login

Print 5

Someone should be dizzy watching it
Now analyze
For example, $a[0][1][1]; The element in the rightmost square bracket represents the innermost element in the array
$a=array(array(array(1,2,3),array (4,5,6)),array(array(7,8,9)));
is divided into up to 3 levels. The element in the rightmost bracket represents the innermost layer
and then to the left The square brackets are moved to the outer layer, and so on

In fact, you will understand if you look at it more. The several layers are divided into several dimensional arrays
In addition, you don’t need to understand the 3-dimensional array too thoroughly. Generally, you can master it. Two-dimensional is enough

I didn’t mention the foreach loop in detail before. In fact, it’s best to use the foreach loop to traverse a one-dimensional array

<?php
$a=array(1,"hello",&#39;a&#39;); 
foreach($a as $value)
echo $value."<br/>"; 
?>
Copy after login

Output

1
hello
a
Copy after login

Isn’t it very simple?
$value is just a temporary variable, used to save array elements. You can call it whatever you want
It is equivalent to giving an array to the proxy variable and letting it help output


<?php
$a=array(1,"hello",&#39;a&#39;); foreach($a as $value)
echo $a."<br/>"; ?>
Copy after login

This will not output the array elements
Only output

Array
Array
Array
Copy after login

The above is the content of PHP Learning Official Set sail (3), more For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement of this Website
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

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1671
14
PHP Tutorial
1276
29
C# Tutorial
1256
24
PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

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

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 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.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 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.

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.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 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.

See all articles