0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

二次元配列から特定のkeyの値を抽出する方法

Last updated at Posted at 2025-07-10

PHPで多次元配列から特定のキーだけを抽出する方法はいくつかありますが、array_column関数を使用すると簡潔に実現できます。

サンプルコードは以下のようになります。

サンプルコード

$records = [
    ['id' => 140, 'key' => '備考', 'value' => 'テスト'],
    ['id' => 141, 'key' => '苗字', 'value' => '田中'],
    ['id' => 142, 'key' => '名前', 'value' => '太郎'],
    ['id' => 143, 'key' => 'メールアドレス', 'value' => 'tanakataro@gmail.com']
];

// idだけを抽出する
$ids = array_column($records, 'id');

$idsの出力結果は以下の通り。

Array
(
    [0] => 140
    [1] => 141
    [2] => 142
    [3] => 143
)

特定のkeyの値の配列を変数として持たせたいときに使えるメソッドです。
使う機会があったら、ぜひ試してみてください。
お役に立てれば嬉しいです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?