This article will give you a detailed introduction to the PHP basic introductory tutorial on PHP arrays. Friends who need to learn how to use PHP arrays can refer to it.
What is an array?
In the process of developing with PHP, sooner or later, you will need to create many similar variables.
Instead of having many similar variables, you can store data as elements in an array.
Elements in the array have their own IDs, so they can be accessed easily.
There are three array types:
Numeric array
Array with numeric ID keys
Associative array
Each ID key in the array is associated with a value
Multidimensional array
Array containing one or more arrays
Numeric array
Numeric arrays store each element with a numeric ID key.
Different methods can be used to create numeric arrays:
Example 1
In this example, the ID key is automatically assigned:
The code is as follows | Copy code | ||||
|
代码如下 | 复制代码 |
$names[0] = "Peter";
$names[0] = "Peter"; echo $names[1] . " and " . $names[2] . " are ". $names[0] . "'s neighbors"; 以上代码的输出: Quagmire and Joe are Peter's neighbors |
The code is as follows | Copy code | ||||||||||||||||||||||||||||||||||||||||
$names[0] = "Peter"; $names[0] = "Peter";
$names[1] = "Quagmire";
?>
Quagmire and Joe are Peter's neighbors
Multidimensional array Example 1
"Megan" Array ( [Griffin] => Array
(
[0] => Peter
[1] => Lois
[2] => Megan
)
[Quagmire] => Array
(
[0] => Glenn
)
[Brown] => Array
(
[0] => Cleveland
[1] => Loretta
[2] => Junior
)
)
Example 2
Let’s try to display a single value from the array above:
Related labels:
source:php.cn
Previous article:Some uncommon methods of for loop statements in PHP_PHP Tutorial
Next article:Detailed explanation of PHP assignment to array (two-dimensional array assignment)_PHP tutorial
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
Latest Articles by Author
Latest Issues
Group MySQL results by ID for looping over
I have a table with flight data in mysql. I'm writing a php code that will group and displ...
From 2024-04-06 17:27:56
0
1
406
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|