PHP Study Notes (d)

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

  1. First of all, do not have to declare an array, can be defined directly.
  2. PHP support while, do ... while, for loop, and the subscript starts at 0.
  3. With echo output, only a one output array can not be directly used loop.
  4. 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:
Here Insert Picture Description
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:
Here Insert Picture Description
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.

Published 37 original articles · won praise 2 · Views 1424

Guess you like

Origin blog.csdn.net/weixin_44377940/article/details/88544050