1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PHP 配列に格納された連想配列に要素を追加する

Posted at

目的

  • 配列に格納された連想配列に値を追加する際に若干詰まったので方法をまとめる

前提情報

具体例

  • 連想配列が格納された配列$testsを定義し、$testsの末尾に連想配列を追加する処理を下記に記載する。

    <?php
    
    //連想配列が格納された配列の定義
    $tests = [
        [
            'str_1' => 'aaa',
            'str_2' => 'bbb',
            'str_3' => 'ccc',
        ]
    ];
    
    //追加する連想配列を記載する
    $tests[] =
        [
            'str_1' => 'ddd',
            'str_2' => 'eee',
            'str_3' => 'fff',
        ];
    
    //配列内容の出力
    foreach ($tests as $test) {
        echo $test['str_1'] ."\n";
        echo $test['str_2'] ."\n";
        echo $test['str_3'] ."\n";
        echo '===================';
    };
    ?>
    
1
1
2

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?