php去掉陣列中的空元素的方法:1.使用「function myfunction($v) {...}」方法去除空元素;2、使用「foreach ($urlArr as $key => ; $val) {...}”方法去掉數組中的空元素。
本文操作環境:windows7系統、PHP7.1版,DELL G3電腦
php怎麼去掉陣列中的空元素?
PHP陣列去除空元素的方法
去移除空元素方法一:(保持鍵值不變)
原始陣列:
<?php Array( [0] => sdfdsdsdsfrrrrrrr [1] => fdsfdsdsfds [2] => fdsfdsdsfdsds [3] => fdsdsfdsfds [4] => [5] => [6] => [7] => fdsfdsfdsfds [8] => fdsfdsfdsfdsfdsfsd [9] => ssssssss [10] => [11] => sssssssssssssssssssss) ?>
處理後陣列
<?php Array( [0] => sdfdsdsdsfrrrrrrr [1] => fdsfdsdsfds [2] => fdsfdsdsfdsds [3] => fdsdsfdsfds [7] => fdsfdsfdsfds [8] => fdsfdsfdsfdsfdsfsd [9] => ssssssss [11] => sssssssssssssssssssss) ?>
使用方法:
<?php function myfunction($v) { if ($v != "") { return true; } return false; } print_r(array_filter($urlArr, "myfunction")); ?>
移除空元素方法二:(鍵值自增)
原始陣列:
<?php Array ( [0] => sdfdsdsdsfrrrrrrr [1] => fdsfdsdsfds [2] => fdsfdsdsfdsds [3] => fdsdsfdsfds [4] => [5] => [6] => [7] => fdsfdsfdsfds [8] => fdsfdsfdsfdsfdsfsd [9] => ssssssss [10] => [11] => sssssssssssssssssssss )
處理之後陣列:
Array ( [0] => sdfdsdsdsfrrrrrrr [1] => fdsfdsdsfds [2] => fdsfdsdsfdsds [3] => fdsdsfdsfds [4] => fdsfdsfdsfds [5] => fdsfdsfdsfdsfdsfsd [6] => ssssssss [7] => sssssssssssssssssssss )
使用方法:
$arr = array(); foreach ($urlArr as $key => $val) { if (empty($val)) { continue; } $arr[] = $val; }
推薦學習:《PHP影片教學》
以上是php怎麼去掉陣列中的空元素的詳細內容。更多資訊請關注PHP中文網其他相關文章!