8
8

More than 5 years have passed since last update.

二次元配列の表から指定されたIDの行を見つけ出したい

Last updated at Posted at 2014-05-23

業務で頻発するのですが、以下のような場合ですね。

<?php

// 表
$users = [
  ['id' => 1, 'name' => '長宗我部'],
  ['id' => 2, 'name' => '勅使河原'],
  ['id' => 3, 'name' => '小比類巻']
];

// 見つけ出したいID
$id = 2;

foreach ($users as $user) {
  if ($user['id'] == $id) {
    echo $user['name']; break;
  }
}
結果
勅使河原

配列系の強いPHPならもっとシンプルに書けると思うのですが、頭が固くで思いつきませんでした。

良い書き方がありましたら教えてくださいm(__)m

8
8
7

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