- 投稿日 : 2008-09-09
- 更新日 : 2010-01-06
- PHP
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";
}
?>
Comments:0
Trackbacks:0
- Trackback URL for this entry
- http://bowz.info/1774/trackback
- Listed below are links to weblogs that reference
- [ PHP の文法 ] 配列 from Bowz::Notebook
Additional comments powered by BackType