Array
Direct look at the code:
<?php
$a = array('English','China','Canada');
echo "$a\n";
for($i=0;$i<3;$i++)
{
echo "$a[$i]\n";
}
$a[4] = 'Franch';
echo "$a[4]";
?>
Run results shown in Figure:
the amount of information is slightly larger
- First of all, do not have to declare an array, can be defined directly.
- PHP support while, do ... while, for loop, and the subscript starts at 0.
- With echo output, only a one output array can not be directly used loop.
- Array elements can be added directly behind the support.
Again:
<?php
$a = array('English','China','Canada');
foreach ($a as $i)
{
echo $i."\n";
}
$b = array('banana'=>100, 'apple'=>200, 'pear'=>300);
echo $b['banana'];
?>
Run results shown in Figure:
PHP also has foreach statement.
PHP supports changing the index, the default index 0,1,2, ......, use the "=>" key and the value associated with the change. And the index value is a string, to single quotes. Another method:
<?php
$a = array('a'=>'England','b'=>'China','c'=>'Canada');
$b = array('c'=>'Franch','d'=>'German','e'=>'Russia');
$c = $a+$b;
while($i = each($c))
{
echo $i['key']." - ".$i['value']."\n";
}
?>
Run results shown in Figure:
the each statement of the obvious role and usage, specific look at the code.
"+" Two arrays can be connected together, when a key is the same, without the old value.