PHP learning officially sets sail (3)
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}; ?>
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(); ?>
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; ?>
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]; ?>
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/>"; ?>
count keyword to calculate how many there are in an array. elements
Another option is to
<?php $a[0]='a'; $a[1]='b'; $a[2]='c'; for($i=0;$i<count($a);$i++) echo $a[$i]."<br/>"; ?>
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]='a'; $a[1]='b'; $a[2]='c'; $a[5]='d'; for($i=0;$i<count($a);$i++) echo $a[$i]."<br/>"; ?>
Also the elements in the array can not be of the same type
<?php $a=array(1,'b',"hello",1.0); for($i=0;$i<count($a);$i++) echo $a[$i]."<br/>"; ?>
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,'b'=>2,"c"=>3); echo $a["a"]."<br/>"; echo $a['b']."<br/>"; echo $a["c"]."<br/>"; ?>
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,'a'=>2,"c"=>3); echo $a["a"]."<br/>"; echo $a["c"]."<br/>"; ?>
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/>"; ?>
But the following is wrong
<?php $a["a"]=>"hello"; $a["b"]=>"world"; echo $a["a"]."<br/>"; echo $a["b"]."<br/>"; ?>
In addition, numbers can also be used as keys
<?php $a["1"]="hello"; $a["2"]="world"; echo $a["1"]."<br/>"; echo $a["2"]."<br/>"; ?>
is feasible
It can be output without quotation marks, but php has a prompt to note, do not use this
<?php $a['a']="hello"; $a['b']="world"; echo $a[a]."<br/>"; echo $a [ b ]"; ?>
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('a',1,2),array("hello",3,1.1,)); echo $a[0][0]; ?>
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('a',1,2),array("hello",3,1.1,2,'a')); echo $a[0][0]; ?>
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/>"; } ?>
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('a'=>array('a'=>"hello",'b'=>"world"),'b'=>array('one'=>1,'two'=>2,'three'=>3)); echo $a['a']['a']; ?>
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('a'=>array("hello","world"),'b'=>array(1,2,3)); echo $a['a'][0]."<br/>".$a['b'][2]; ?>
print out
hello 3
must not be played like this
<?php $a=array(array('a'=>"hello",'b'=>"world"),array('one'=>1,'two'=>2,'three'=>3)); echo $a['a']; ?>
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]; ?>
Print 1
<?php $a=array(array(array(1,2,3)),array(array(4,5,6))); echo $a[1][0][0]; ?>
Print 4
<?php $a=array(array(array(1,2,3),array(4,5,6)),array(array(7,8,9))); echo $a[0][1][1]; ?>
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",'a'); foreach($a as $value) echo $value."<br/>"; ?>
Output
1 hello a
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",'a'); foreach($a as $value) echo $a."<br/>"; ?>
This will not output the array elements
Only output
Array Array Array
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)!

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











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 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 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 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 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 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 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.
