【PHP】配列おさらい〜連想配列の編集と代入編

前回記事の続きです。

>[【PHP】配列おさらい〜結合編](https://qiita.com/33yuki/items/242e55a9a69a4a7fa3db)

# 使用する配列

こちらのサイトから拝借。（少し編集しました。）

>[FuelPHP1.7 Documentation ~ Arr クラス](http://fuelphp.jp/docs/1.7/classes/arr.html)

```test.php
$col_1 = array(
    'id' => 1,
    'name' => 'Bill',
    'age' => 33,
    'country' => 'America',
);

$col_2 = array(
    'id' => 2,
    'name' => 'Chris',
    'age' => 54,
    'country' => 'Canada',
);

$collection = array(
    array(
        'id' => 1,
        'name' => 'Bill',
        'age' => 33,
        'country' => 'America',
    ),
    array(
        'id' => 2,
        'name' => 'Chris',
        'age' => 54,
        'country' => 'Canada',
    ),
    array(
        'id' => 3,
        'name' => 'Bert',
        'age' => 42,
        'country' => 'America',
    ),
    array(
        'id' => 4,
        'name' => 'Taro',
        'age' => 33,
        'country' => 'Japan',
    ),
);
```

```test.php
print_r($col_1);

/* 結果
Array
(
    [id] => 1
    [name] => Bill
    [age] => 33
    [country] => America
)
*/

print_r($col_2);

/* 結果
Array
(
    [id] => 2
    [name] => Chris
    [age] => 54
    [country] => Canada
)
*/

print_r($collection);

/* 結果
Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Bill
            [age] => 33
            [country] => America
        )

    [1] => Array
        (
            [id] => 2
            [name] => Chris
            [age] => 54
            [country] => Canada
        )

    [2] => Array
        (
            [id] => 3
            [name] => Bert
            [age] => 42
            [country] => America
        )

    [3] => Array
        (
            [id] => 4
            [name] => Taro
            [age] => 33
            [country] => Japan
        )

)
*/
```

# 特定のキーごとに連想配列をまとめる

特定のキー（ここではcountry）ごとに配列をまとめます。

これは自分で解決できず、Google先生に頼ることに。以下の記事に救われました。

>[[php]連想配列を特定のキーの同じ値でグルーピング](http://blog.code-life.net/blog/2013/12/18/php-group-array-by-value/)

```test.php
$country_group = array();

foreach ($collection as $key => $value) {
    $c_name = $value['country'];

    if (!isset($country_group[$c_name])) {
        $country_group[$c_name] = array();
    }

    $country_group[$c_name][] = $value;
}

print_r($country_group);

/* 結果
Array
(
    [America] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [name] => Bill
                    [age] => 33
                    [country] => America
                )

            [1] => Array
                (
                    [id] => 3
                    [name] => Bert
                    [age] => 42
                    [country] => America
                )

        )

    [Canada] => Array
        (
            [0] => Array
                (
                    [id] => 2
                    [name] => Chris
                    [age] => 54
                    [country] => Canada
                )

        )

    [Japan] => Array
        (
            [0] => Array
                (
                    [id] => 4
                    [name] => Taro
                    [age] => 33
                    [country] => Japan
                )

        )

)
*/
```

# 連想配列を要素とする配列を作る

## 配列名[]で代入する

``[]``を忘れると上書きされてしまいます。

```test.php
$col_0 = array();
$co1_0[] = $col_1; 
$co1_0[] = $col_2;

print_r($co1_0);   

/* 結果
Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Bill
            [age] => 33
            [country] => America
        )

    [1] => Array
        (
            [id] => 2
            [name] => Chris
            [age] => 54
            [country] => Canada
        )

)
*/
```

## foreach文で回す場合

上書きされるのではと思ったら、自動的に``$collection_2``の添字のカウントが進んでいく模様。

```test.php
$collection_2 = array();

foreach ($collection as $key => $value) {
    $collection_2[$key] = $value;
}

print_r($collection_2); 

/* 結果
Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Bill
            [age] => 33
            [country] => America
        )

    [1] => Array
        (
            [id] => 2
            [name] => Chris
            [age] => 54
            [country] => Canada
        )

    [2] => Array
        (
            [id] => 3
            [name] => Bert
            [age] => 42
            [country] => America
        )

    [3] => Array
        (
            [id] => 4
            [name] => Taro
            [age] => 33
            [country] => Japan
        )

)
*/
```
  
## array_pushを使用する

前回の記事でも紹介しました。

>[【PHP】配列おさらい〜結合編〜array_push](https://qiita.com/33yuki/items/242e55a9a69a4a7fa3db#%E9%80%A3%E6%83%B3%E9%85%8D%E5%88%97-1)

``空でない配列``に追加しようとすると、連想配列ではキーと要素のバランスがおかしくなってしまいます・・・

# 配列に配列を代入する

## 配列名 = 配列名

元の配列と全く同じ形です。

```test.php
$retcol_1 = $col_1;

print_r($retcol_1);

/* 結果
Array
(
    [id] => 1
    [name] => Bill
    [age] => 33
    [country] => America
)
*/ 
```

## 配列名[] = 配列名

配列自体が１つの要素として扱われます。

```test.php
$retcol_2[] = $col_1;

print_r($retcol_2); 

/* 結果
Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Bill
            [age] => 33
            [country] => America
        )

)
*/ 
```

次回に続きます。