PHP の文法 [ 配列 ] についての解説です。
配列 ( array ) ———————————————————————-
<?php $fruit[] = "Apple";
$fruit[] = "Orange";
$fruit[] = "Grape";
print "$fruit[0]<br>\n”;
print “$fruit[1]<br>\n”;
print “$fruit[2]<br>\n”;
?>
※ 配列の先頭は常に $fruit[0]
になるみたいです。
<?php $fruit = array ( "Apple", "Orange", "Grape" );
print "$fruit[0]<br>\n”;
print “$fruit[1]<br>\n”;
print “$fruit[2]<br>\n”;
?>
↑ こんな書き方もあります。
<?php $fruit = explode ( ",", "Apple,Orange,Grape" );
print "$fruit[0]<br>\n”;
print “$fruit[1]<br>\n”;
print “$fruit[2]<br>\n”;
?>
↑ 上の例では、コンマを区切り文字として配列に代入している。
連想配列 ———————————————————————-
インデックスが文字列の配列の場合を [ 連想配列 ] と呼ぶんだそうです。
<?php $fruit = array (
"Apple" => “りんご”,
“Orange” => “みかん”,
“Grape” => “ぶどう”
);
foreach ( $fruit as $key => $value ) {
print “$key – $value<br>\n”;
}
?>