5.配列
 1)一次元配列
   一次元配列の形式
   形式:$配列変数名[キー]
       キーには、添数(インデックス)と文字列が使えます。
       インデックスを使った場合は、インデックスが「0」から始まります。
       文字列を使った場合は、「連想配列」といいます。

  ①配列のインデックスを省略した場合
    配列のインデックスを省略した場合は、自動的に「0」始まります。
   ファイル名:php5-1-1.php

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>1次元配列(配列のインデックスを省略した場合)</title>
</head>
<body>
<?php
$ar[]=10;
$ar[]=20;
echo "合計:".$ar[0]+$ar[1];
?>
</body>
</html>
  ②配列のインデックスを指定した場合
    配列のインデックスを指定した場合は、そのインデックスの配列が生成されます。
    PHPでは、配列にアクセスしたときにその配列が生成されるため値を代入する前に値を取り出してもエラーになりません。
   ファイル名:php5-1-2.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>1次元配列(配列のインデックスを指定した場合)</title>
</head>
<body>
<?php
$ar[1]=10;
$ar[2]=20;
echo '配列$ar[0]='.$ar[0]."<br>";
echo "合計:%d<br>",$ar[1]+$ar[2];
echo '配列$ar[3]='.$ar[3];
?>
</body>
</html>
 2)多次元配列
   多次元配列では、$ar[0][[0][0]のようにして3次元以上の配列を使うことはできますが、ほとんどの場合が2次元配列までです。
   ここでは、2次元配列について説明します。
   2次元配列は、Excelのようは表になっていると考えると分かりやすいと思います。
   たとえば、

     $ar[行インデックス][列インデックス] または、$ar[列インデックス][行インデックス] と考えると分かりやすいです。

 
   以下の例では、上記の前者で配列を表と考えたものです。使って表す時には、2次元配列が使われます。

  
  $ar[][0] $ar[][1] $ar[][2]
$ar[0][] $ar[0][0] $ar[0][1] $ar[0][2]
$ar[1][] $ar[1][0] $ar[1][1] $ar[1][2]
$ar[2][] $ar[2][0] $ar[2][1] $ar[2][2]
  $ar[][0] $ar[][1] $ar[][2]
$ar[0][] 1 2 3
$ar[1][] 4 5 9
$ar[2][] 5 7 12

    上右表は、行が下に移動すると行インデックスが+1カウントされており、列が右に移動すると列インデックスが+1カウントされています。

   ファイル名:php5-2-1.php

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>二次配列の計算</title>
</head>
<body>
二次配列の計算<br>
<?php
$ar[0][0]=1;
$ar[0][1]=2;
$ar[1][0]=4;
$ar[1][1]=5;
$ar[0][2]=$ar[0][0]+$ar[0][1];
$ar[1][2]=$ar[1][0]+$ar[1][1];
$ar[2][0]=$ar[0][0]+$ar[1][0];
$ar[2][1]=$ar[0][1]+$ar[1][1];
$ar[2][2]=$ar[2][0]+$ar[2][1];
echo "<table width='200' border='1'>
<tr>
<th nowrap='nowrap'>&nbsp;</th>
<th nowrap='nowrap'>".'$ar[][0]'."</th>
<th nowrap='nowrap'>".'$ar[][1]'."</th>
<th nowrap='nowrap'>".'$ar[][2]'."</th>
</tr>
<tr>
<th nowrap='nowrap'>".'$ar[0][]'."</th>
<td nowrap='nowrap' align='right'>".$ar[0][0]."</td>
<td nowrap='nowrap' align='right'>".$ar[0][1]."</td>
<td nowrap='nowrap' align='right'>".$ar[0][2]."</td>
</tr>
<tr>
<th nowrap='nowrap'>".'$ar[1][]'."</th>
<td nowrap='nowrap' align='right'>".$ar[1][0]."</td>
<td nowrap='nowrap' align='right'>".$ar[1][1]."</td>
<td nowrap='nowrap' align='right'>".$ar[1][2]."</td>
</tr>
<tr>
<th nowrap='nowrap'>".'$ar[2][]'."</th>
<td nowrap='nowrap' align='right'>".$ar[2][0]."</td>
<td nowrap='nowrap' align='right'>".$ar[2][1]."</td>
<td nowrap='nowrap' align='right'>".$ar[2][2]."</td>
</tr>
</table>";
?>
</body>
</html>
 3)連想配列
   連想配列は、配列のキーが文字列にしたものをいいます。文字列にすることで配列に入っているデータが直感的にわかるようになります。
   しかし、インデックスを使った配列のように数値を指定することで配列の操作ができない欠点があります。

   ファイル名:php5-3.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>連想配列</title>
</head>
<body>
連想配列<br>
<?php
$renArray['name']="山田 太郎";
$renArray['zip']="533-0000";
$renArray['address']="大阪市東淀川区豊新 5-23-45";
$renArray['tel']="00-0000-0000";
$renArray['email']="yamada@abc.com";
echo "氏  名:".$renArray['name']."<br>".
"郵便番号:".$renArray['zip']."<br>".
"住  所:".$renArray['address']."<br>".
"電話番号:".$renArray['tel']."<br>".
"E-Mail :".$renArray['email'];
?>
</body>
</html>
 4)多次元配列と連想配列
   キーに文字列とインデックスを使った例を以下にしまします。以下のようにするとデータベースの表のように列名があるかのように
  振る舞うことができます。
   下記のプログラムを実行すると下表が表示されます。
   
氏名(1列目) 郵便番号(2列目) 住所(3列目) 電話番号(4列目) E-Mail(4列目)
1行目 山田 太郎 533-0000 大阪市東淀川区豊新 5-23-45 00-0000-0000 yamada@abc.com
2行目 鈴木 恵子 533-0011 大阪市東淀川区山田 4-3-12 12-3456-7890 suzuki@abc.com
3行目 佐藤 五郎 533-1234 大阪市東淀川区港 5-3-5 09-8765-4321 sato@abc.com

  ファイル名:php5-4.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>多次元配列と連想配列</title>
</head>
<body>
多次元配列と連想配列<br>
<?php
$renArray[0]['name']="山田 太郎";
$renArray[0]['zip']="533-0000";
$renArray[0]['address']="大阪市東淀川区豊新 5-23-45";
$renArray[0]['tel']="00-0000-0000";
$renArray[0]['email']="yamada@abc.com";
$renArray[1]['name']="鈴木 恵子";
$renArray[1]['zip']="533-0011";
$renArray[1]['address']="大阪市東淀川区山田 4-3-12";
$renArray[1]['tel']="12-3456-7890";
$renArray[1]['email']="suzuki@abc.com";
$renArray[2]['name']="佐藤 五郎";
$renArray[2]['zip']="533-1234";
$renArray[2]['address']="大阪市東淀川区港 5-3-5";
$renArray[2]['tel']="09-8765-4321";
$renArray[2]['email']="sato@abc.com";
echo "<table border='1'>
<tr>
<th nowrap='nowrap'>&nbsp;</th>
<th nowrap='nowrap'>氏名(1列目)</th>
<th nowrap='nowrap'>郵便番号(2列目)</th>
<th nowrap='nowrap'>住所(3列目)</th>
<th nowrap='nowrap'>電話番号(4列目)</th>
<th nowrap='nowrap'>E-Mail(4列目)</th>
</tr>
<tr>
<th nowrap='nowrap'>1行目</th>
<td nowrap='nowrap'>".$renArray[0]['name']."</td>
<td nowrap='nowrap'>".$renArray[0]['zip']."</td>
<td nowrap='nowrap'>".$renArray[0]['address']."</td>
<td nowrap='nowrap'>". $renArray[0]['tel']."</td>
<td nowrap='nowrap'>".$renArray[0]['email']."</td>
</tr>
<tr>
<th nowrap='nowrap'>2行目</th>
<td nowrap='nowrap'>".$renArray[1]['name']."</td>
<td nowrap='nowrap'>".$renArray[1]['zip']."</td>
<td nowrap='nowrap'>".$renArray[1]['address']."</td>
<td nowrap='nowrap'>". $renArray[1]['tel']."</td>
<td nowrap='nowrap'>".$renArray[1]['email']."</td>
</tr>
<tr>
<th nowrap='nowrap'>3行目</th>
<td nowrap='nowrap'>".$renArray[2]['name']."</td>
<td nowrap='nowrap'>".$renArray[2]['zip']."</td>
<td nowrap='nowrap'>".$renArray[2]['address']."</td>
<td nowrap='nowrap'>". $renArray[2]['tel']."</td>
<td nowrap='nowrap'>".$renArray[2]['email']."</td>
</tr>
</table>";
?>
</body>
</html>