BLOG

[PHP] 配列内の空白を削除「array_filter」「array_diff」

| PHP

配列内の空白を削除したい場合の関数を紹介。
”, null, 0とかの値を削除です。

array_filter

$array = array('test1', 'test2', 0, '', null, 'test3', 'test4', 'test5');
$result = array_filter($array);
Array
(
  [0] => 'test1'
  [1] => 'test2'
  [5] => 'test3'
  [6] => 'test4'
  [7] => 'test5'
)

array_diff

指定した数値を消す

$array = array('test1', 'test2', 0, '', null, 'test3', 'test4', 'test5');
$result = array_diff($array, array("",0,null));
Array
(
  [0] => 'test1'
  [1] => 'test2'
  [5] => 'test3'
  [6] => 'test4'
  [7] => 'test5'
)
PAGE TOP