LoginSignup
24
34

More than 5 years have passed since last update.

PHP 配列 連想配列 foreach 二次元配列

Last updated at Posted at 2016-10-26

配列


連想配列
keyを指定すれば連想配列
→keyを指定する際は意味のある文字列にすること
(数値でも動作に問題はないが、何の値かは分かり辛いので文字列のみの認識でOK)


多次元配列(2次元配列・3次元配列)
配列の中に配列が入ってるのを多次元配列と呼ぶ。(2次元・3次元とかはその階層)

1次元配列は配列の中に配列が入っていないものを指す

array関数

▪️array
配列を生成する

2通りある。
・array関数を使って配列を作成する方法
・[]を使って配列を作成する方法

■例

<?php
$color = array (
        "red",
        "blue",
        "green"
);
echo $color [0];
var_dump ( $color );

$weather [] = "rainy";
$weather [] = "sunny";
$weather [] = "cloudy";
echo $weather [1];
?>

■例 実行結果
redarray (size=3)
0 => string 'red' (length=3)
1 => string 'blue' (length=4)
2 => string 'green' (length=5)
sunny

キーが飛び飛びの値でもOK

キー値を指定して追加することも出来る

■例

<?php
$weather [] = "rainy";
$weather [] = "sunny";
$weather [] = "cloudy";

echo $weather [1];
$weather [5] = "rainy with sunny";

echo $weather [5];
$weather [4] = "snowy";
echo $weather [4];
var_dump ( $weather );
?>

■実行結果
sunnyrainy with sunnysnowyarray (size=5)
0 => string 'rainy' (length=5)
1 => string 'sunny' (length=5)
2 => string 'cloudy' (length=6)
5 => string 'rainy with sunny' (length=16)
4 => string 'snowy' (length=5)

連想配列


・連想配列はキーが文字列、数字で指定されている(意味が通るもので指定されている)
キーを指定していない→普通の配列
キーを指定している→連想配列
・連想配列は2通りの方法で作成することが出来る
①array関数、「=>」を使う方法 ②[]を使う方法
・連想配列も通常の配列と同様の方法で作成や値の追加が出来る
・arrayで作るとき「キー => 値」という形式で指定する
・[]で作るときは「=>」は使わない
「=>」はダブルアロー演算子

■例

        <?php
                    $i = 'test';
                    echo "ここは$i";
                    echo 'ここは' . $i;
                    echo "ここは" . $i;

                    // ①array関数、「=>」を使う方法
                    $score = array (
                            'math' => 30,
                            "national_language" => 100,
                            "English" => 45
                    );
                    // ②[]を使う方法
                    $score_test ["math"] = 30;
                    $score_test ["national_language"] = 100;
                    $score_test ["English"] = 45;

                    $score = array (
                            "math" => array (
                                    30 => $score_test
                            ),
                            "national_language" => 100,
                            "English" => 45
                    );
                    echo "国語の点数は" . $score ["national_language"] . "点";
                    var_dump ( $score );
                    // ①追加
                    $score ["national_language"] = 50;
                    echo "国語の点数は" . $score ['national_language'] . "点";
                    echo "<br>";

                    // ②[]を使う方法
                    $score1 ["math"] = 30;
                    $score1 ["national_language"] = 100;
                    $score1 ["English"] = 45;

                    $german ["grammer"] = 20;
                    $german ["adjective"] = 30;
                    $german ["verb"] = 50;

                    echo "英語の点数は" . $score1 ["English"] . "点";
                    // echo $score1 [45];←NG
                    var_dump ( $score1 );
                    // ②追加
                    $score1 ["English"] = 60;
                    echo "英語の点数は" . $score1 ["English"] . "点";
                    $score1 ["language"] = array (
                            "German" => 54,
                            "Spanish" => 77,
                            "Chinese" => 86
                    );
                    $score1 ["German"] = $german;
                    var_dump ( $german );

                    $students = array (
                            "A-san",
                            "B-san",
                            "C-san"
                    );
                    var_dump ( $students );
                    $students = $student ["D-san"] = $german;
                    var_dump ( $students );
                    // var_dump ( $students ["D-san"] );
                    // 一次元配列
                    $blood_type = array (
                            "A",
                            "B",
                            "O",
                            "AB",
                            8 => "?"
                    );
                    // var_dump ( $blood_type );
                    // 初期化
                    $test_student = array ();
                    // 連想配列
                    $test_student = array (
                            'shincho' => '170.5cm',
                            'taiju' => '56kg',
                            'blood_type' => 'A'
                    );
                    // 一次元配列
                    $test_student2 = array (
                            'shincho' => '171.5cm',
                            'taiju' => '66kg',
                            'blood_type' => 'B'
                    );

                    $test_students ['test_student'] [] = $test_student;
                    $test_students ['test_student'] [] = $test_student2;
                    var_dump ( $test_students );
                    ?>

■実行結果
ここはtestここはtestここはtest国語の点数は100点array (size=3)
'math' =>
array (size=1)
30 =>
array (size=3)
'math' => int 30
'national_language' => int 100
'English' => int 45
'national_language' => int 100
'English' => int 45
国語の点数は50点
英語の点数は45点array (size=3)
'math' => int 30
'national_language' => int 100
'English' => int 45
英語の点数は60点array (size=3)
'grammer' => int 20
'adjective' => int 30
'verb' => int 50
array (size=3)
0 => string 'A-san' (length=5)
1 => string 'B-san' (length=5)
2 => string 'C-san' (length=5)
array (size=3)
'grammer' => int 20
'adjective' => int 30
'verb' => int 50
array (size=1)
'test_student' =>
array (size=2)
0 =>
array (size=3)
'shincho' => string '170.5cm' (length=7)
'taiju' => string '56kg' (length=4)
'blood_type' => string 'A' (length=1)
1 =>
array (size=3)
'shincho' => string '171.5cm' (length=7)
'taiju' => string '66kg' (length=4)
'blood_type' => string 'B' (length=1)

for構文と配列

$students = array (
        "math",
        "society",
        "English",
        "Japanese"
);
for($i = 0; $i < 4; $i ++) {
    echo $students [$i] . "<br>";
}

■実行結果
math
society
English
Japanese

foreach構文

-foreach は、配列を反復処理するための便利な方法

<?php
$students = array ();
$students = array (
        "math",
        "society",
        "English",
        "Japanese"
);
for($i = 0; $i < 4; $i ++) {
    echo $students [$i] . "<br>";
}

$students1 = array (
        "math" => 80,
        "English" => 100,
        "society" => 70
);
foreach ( $students as $a ) {
    echo $a . "<br>";
}

foreach ( $students as $b => $c ) {
    echo "キーは" . $b . "で、valueは" . $c . "です<br>";
}
foreach ( $students1 as $a => $b ) {
    echo "キーは" . $a . "で、valueは" . $b . "です<br>";
}

foreach ( $students as $a ) :
    echo $a . "<br>";
endforeach
;

foreach ( $students as $a => $b ) :
    echo "キーは" . $a . "で、valueは" . $b . "です<br>";
endforeach
;
?>
■実行結果
math
society
English
Japanese
math
society
English
Japanese
キーは0で、valueはmathです
キーは1で、valueはsocietyです
キーは2で、valueはEnglishです
キーは3で、valueはJapaneseです
キーはmathで、valueは80です
キーはEnglishで、valueは100です
キーはsocietyで、valueは70です
math
society
English
Japanese
キーは0で、valueはmathです
キーは1で、valueはsocietyです
キーは2で、valueはEnglishです
キーは3で、valueはJapaneseです

配列での'と"の違い

配列での'と"の違い
$testArray = array (
        "a",
        "b",
        "c"
);
echo "$testArray[0]";
echo '</br>';
echo $testArray ['2'];
echo '</br>';
echo $testArray[2];
実行結果
a
c 
c

array ($id)ナニコノarrayの形?

$val = 'aaa';
$arr = array($val);
var_dump($arr); //'aaa'がセットされている

$val = 'bbb';
var_dump($arr); //'aaa'のまま
$arr = array($val); //'bbb'のまま
var_dump($arr);
実行結果
array (size=1)
  0 => string 'aaa' (length=3)

array (size=1)
  0 => string 'aaa' (length=3)

array (size=1)
  0 => string 'bbb' (length=3)

参考サイト

二次元配列

test30.php
<?php
$fruit = array ();
$fruit [] = array (
        "a" => "aaaaaa",
        "b" => "bbbbb",
        "c" => "ccccc"
);
// echo $fruit[0]["a"];
$fruit ['a'] = array (
        "math" => "98",
        "English" => "100",
        "Japanese" => "55"
);
echo $fruit ['a'] ['math'];
echo "</br>";

$customer ['hoge'] = array (
        "id" => "ididid",
        "name" => "namenamename",
        "address" => "addressaddress"
);
echo $customer ['hoge'] ['name'];
?>

■test30.php 実行結果
98
namenamename

配列コピー

配列をコピーした場合、参照数が増えるだけで実際にデータはコピーされないため、要素数が多い配列をコピーしてもメモリの消費量はあまり増えない

配列の要素の順番

配列の順番は、要素が追加された順番と同じ

test35.php
$colors[0] = 'red';
$colors[1] = 'yellow';
$colors[3] = 'orange';
$colors[2] = 'blue';

foreach ($colors as $color){
    echo "$color";
}
var_dump($colors);
実行結果
redyelloworangeblue
array (size=4)
  0 => string 'red' (length=3)
  1 => string 'yellow' (length=6)
  3 => string 'orange' (length=6)
  2 => string 'blue' (length=4)
24
34
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
34