2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

配列の中のnullの要素を排除する方法

Last updated at Posted at 2020-11-19

<?php
 
function valuecomp($var){
    // null以外の値を返す
    return($var <> null);
}
 
$value_array = [100, 200, null, 400, null];
 
//array_filterを使用してコールバック関数に配列を渡す
$value = array_filter($value_array, 'valuecomp');

var_dump($value);

?>

実行結果
array(3) { [0]=> int(100) [1]=> int(200) [3]=> int(400) }
//配列の番号を詰める場合は、これ

array_values($value);
var_dump($value);

実行結果
array(3) { [0]=> int(100) [1]=> int(200) [2]=> int(400) }
2
1
2

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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?