LoginSignup
0
0

More than 1 year has passed since last update.

PHP 配列の中にある空の要素のみを削除 array_diff()

Posted at

array_diff()を用いることで、スマートに配列の中のからの要素のみを削除できる。
php公式サイト array_diff:https://www.php.net/manual/ja/function.array-diff.php)

$ar = [1 => "レッド", 2 => "", 3 => "ブルー", 4 => "グリーン", 5 => ""];
print_ra($ar);
// 実行結果
// Array(
//    [1] => レッド
//    [2] =>
//    [3] => ブルー
//    [4] => グリーン
//    [5] => 
//)

$ar = array_diff($ar, array(""));
print_ra($ar);
// 実行結果
// Array(
//    [1] => レッド
//    [3] => ブルー
//    [4] => グリーン
//)
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