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?

【PHP】【array_column】配列から特定のカラムかつ特定の値でレコードで絞り込みたい

0
Last updated at Posted at 2025-12-03

多次元配列を指定したキーと値にする方法※array_columnを使う

<?php
$query_data = [
  ["id" => "10","name" => "カレー","submenu_name" => "ライス","drink" => "烏龍茶"],
  ["id" => "20","name" => "パスタ","submenu_name" => "タバスコ","drink" => "ビール"],
  ["id" => "20","name" => "パスタ","submenu_name" => "タバスコ","drink" => "ビール"],
  ["id" => "20","name" => "パスタ","submenu_name" => "フランスパン","drink" => "ハイボール"],
  ["id" => "20","name" => "パスタ","submenu_name" => "フランスパン","drink" => "ハイボール"],
  ["id" => "30","name" => "焼き肉","submenu_name" => "ソース","drink" => "ジンジャエール"]
];


$drink_list = array_column($query_data, 'drink');
$has_beer = in_array('ビール', $drink_list, true);
$has_highball = in_array('レモンサワー', $drink_list, true);


var_dump($drink_list);
var_dump($has_beer);
var_dump($has_highball);

#結果

array(6) {
  [0]=>
  string(9) "烏龍茶"
  [1]=>
  string(9) "ビール"
  [2]=>
  string(9) "ビール"
  [3]=>
  string(15) "ハイボール"
  [4]=>
  string(15) "ハイボール"
  [5]=>
  string(21) "ジンジャエール"
}
bool(true)
bool(false)
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?