Home > PHP > [ PHP の文法 ] 配列

[ PHP の文法 ] 配列

  • 投稿日 : 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

Home > PHP > [ PHP の文法 ] 配列

About

ウェブディレクター
[ Bowz ] のブログ。
Twitter やってます。

Feeds ( RSS 2.0 )
Meta
あわせて読みたいブログパーツ

Return to page top