Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

PHP | array_filter

概要

array_filter

詳細

array_filter関数で配列からフィルタされた結果を配列で取得する

サンプル

#!/usr/bin/env php
<?php
$hoges = array('hoge1', 'hoge1', 'hige', 'hage');
$result = array_filter($hoges, function ($value) {
  return $value == "hoge1";
});
print_r($result);

出力

Array
(
    [0] => hoge1
    [1] => hoge1
)

Rubyと比較

# encoding: utf-8
hoge = "hoge"
hoges = %w{hoge1 hoge1 hige hage}
print hoges.select { |v|v == "hoge1" }