Step by step learn PHP of 8 PHP arrays

  • 2020-03-31 20:22:36
  • OfStack

1. Arrays in PHP
Instead of thinking of an array in PHP as our narrow sense of an "array," I thought I'd split the array into two, one for our regular array and one for our Dictionary.
2. Create an array
If the array does not exist, storing values in the array will create the array.
 
<?php 
$address[0]=' Beijing '; 
$address[1]=' Shanghai '; 
$address[2]=' nanjing '; 
$introduce[' Beijing ']=' The capital, '; 
$introduce[' Shanghai ']=' International metropolis '; 
$introduce[' nanjing ']=' Don't understand the '; 
?> 

A more orthodox approach is to use the array() language structure, which is my preferred approach:
 
<?php 
$address=array(' Beijing ',' Shanghai ',' nanjing '); 
$introduce=array(' Beijing '=>' The capital, ', 
' Shanghai '=>' International metropolis ', 
' nanjing '=>' Don't understand the ' 
); 
?> 

Of course, we can also create an empty array in this way:
 
<?php 
$nullArray=array(); 
?> 

3. Access array elements
Accessing array elements is the same as the traditional way:
 
<?php 
$address=array(' Beijing ',' Shanghai ',' nanjing '); 
$introduce=array(' Beijing '=>' The capital, ', 
' Shanghai '=>' International metropolis ', 
' nanjing '=>' Don't understand the ' 
); 
echo($address[1]); 
echo($introduce[' Shanghai ']); 
?> 

4. Iterate over group elements
The most common way of traversing groups is foreach, which is also more general.
 
<?php 
$address=array(' Beijing ',' Shanghai ',' nanjing '); 
$introduce=array(' Beijing '=>' The capital, ', 
' Shanghai '=>' International metropolis ', 
' nanjing '=>' Don't understand the ' 
); 
foreach($address as $value) 
{ 
echo($value.'<br/>'); 
} 
foreach($introduce as $key=>$value) 
{ 
echo("$key => $value <br/>"); 
} 
?> 

Foreach can easily iterate through arrays, but it has the disadvantage that instead of working directly with the original array, it makes a copy of the original array before iterating through it, resulting in a waste of time and space.
So there's an easy way to do that, which is for.
 
<?php 
$address=array(' Beijing ',' Shanghai ',' nanjing '); 
$introduce=array(' Beijing '=>' The capital, ', 
' Shanghai '=>' International metropolis ', 
' nanjing '=>' Don't understand the ' 
); 
for($i=0;$i<count($address);$i++) 
{ 
echo("$address[$i]<br/>"); 
} 
?> 

This is simple, but it has the disadvantage that you can only traverse indexed arrays, not dictionaries.
As a result, iterator functions are proposed in PHP.
The most common of these is the each() function. Here's a simple example:
 
<?php 
$introduce=array(' City name '=>' introduce ', 
' Beijing '=>' The capital, ', 
' Shanghai '=>' International metropolis ', 
' nanjing '=>' Don't understand the ' 
); 
reset($introduce); 
echo('<table>'); 
while(list($city,$intro)=each($introduce)) 
{ 
echo("<tr><td>$city</td><td>$intro</td>"); 
} 
echo('</table>'); 
?> 

(link: https://www.jb51.net/upload/2010-3/20100305175044726.png)  
By way of explanation, the each() function is used to iterate over groups of elements, similar to our regular iterator. And the great advantage of using the iteration function is that it does not produce a copy of the original array as the foreach language structure does, which is useful when working with large arrays.

Related articles: