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?

More than 3 years have passed since last update.

【php】配列の中身の0と空要素を削除する ※array_filter()を使う

Last updated at Posted at 2021-07-30

処理時間:1758ナノ秒

<?php

$arr =  [1,2,3,'あああ','00456','123abc',-3,0,0,0,''];

$start = hrtime(true); // 計測開始時間

$arr = array_filter($arr);

$end = hrtime(true); 
echo '処理時間:'.($end - $start).'ナノ秒';

var_dump($arr);

?>

もしくは

処理時間:6726ナノ秒

<?php
$arrs =  [1,2,3,'あああ','00456','123abc',-3,0,0,0,''];

$start = hrtime(true); // 計測開始時間

foreach($arrs as $arr ){

    if(!empty($arr)){

        $result[] = $arr;
    };
};

$end = hrtime(true); 
echo '処理時間:'.($end - $start).'ナノ秒';

var_dump($result);
?>

出力結果

array(7) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  string(9) "あああ"
  [4]=>
  string(5) "00456"
  [5]=>
  string(6) "123abc"
  [6]=>
  int(-3)
}
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?