Home  >  Article  >  Backend Development  >  Learn PHP(8) php array step by step_PHP tutorial

Learn PHP(8) php array step by step_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:40:18911browse

1. Arrays in PHP
Rather than understanding arrays in PHP as "arrays" in our narrow sense, I think we might as well divide this array into two, and one is our conventional Array, one is our Dictionary.
2. Create an array
If the array does not exist, storing values ​​in the array will create the array.

Copy code The code is as follows:

$address[0]='Beijing';
$address[1]='Shanghai';
$address[2]='Nanjing';
$introduce['Beijing']='Capital';
$introduce['Shanghai'] ='International Metropolis';
$introduce['Nanjing']='I don’t understand';
?>

There is also a more orthodox method, using array( ) language structure, this is also the way I prefer:
Copy code The code is as follows:

$address=array('Beijing','Shanghai','Nanjing');
$introduce=array('Beijing'=>'Capital',
'Shanghai'=>'International University City',
'Nanjing'=>'I don't understand'
);
?>

Of course we can also create an empty array in this way:
Copy code The code is as follows:

$nullArray=array();
? >

3. Accessing array elements
Accessing array elements is actually the same as the traditional way:
Copy code The code is as follows:

$address=array('Beijing','Shanghai','Nanjing');
$introduce=array(' Beijing'=>'Capital',
'Shanghai'=>'International Metropolis',
'Nanjing'=>'I don't understand'
);
echo($address [1]);
echo($introduce['Shanghai']);
?>

4. Traverse array elements
Traverse array The most commonly used method is foreach, which is also relatively versatile.
Copy code The code is as follows:

$address=array('Beijing','Shanghai ','Nanjing');
$introduce=array('Beijing'=>'Capital',
'Shanghai'=>'International Metropolis',
'Nanjing'=>' Don't understand'
);
foreach($address as $value)
{
echo($value.'
');
}
foreach ($introduce as $key=>$value)
{
echo("$key => $value
");
}
?>

Foreach traverses the array very easily, but it has a disadvantage, that is, it does not directly operate the original array, but makes a copy of the original array before traversing, which causes time and space problems. of waste.
Then there is a simple method, which is for.
Copy code The code is as follows:

$address=array('Beijing','Shanghai ','Nanjing');
$introduce=array('Beijing'=>'Capital',
'Shanghai'=>'International Metropolis',
'Nanjing'=>' Don't understand'
);
for($i=0;$i{
echo("$address[$i]
");
}
?>

Although this is simple, it also has disadvantages, that is, it can only traverse the index array, and there is no way to traverse the dictionary.
So, the iterator function was proposed in PHP.
The most commonly used one is the each() function. Let’s look at a simple example:
Copy the code The code is as follows:

$introduce=array ('City name'=>'Introduction',
'Beijing'=>'Capital',
'Shanghai'=>'International Metropolis',
'Nanjing'=>' Don't understand'
);
reset($introduce);
echo('');
while(list($city,$intro)=each($introduce) )
{
echo("");
}
echo('< /table>');
?>

image
To explain, the each() function is used to traverse array elements, similar to our iterator in the conventional sense. And the biggest advantage of using the iterative function is that it does not produce a copy of the original array like the foreach language structure, which is very useful when dealing with large arrays.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321375.htmlTechArticle1. Arrays in PHP Rather than understanding arrays in PHP as "arrays" in our narrow sense, I think You might as well divide this array into two, one is our regular array, the other is our...
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
$city$intro