ในการลบองค์ประกอบอาร์เรย์ว่างใน PHP โค้ดจะเป็นดังนี้ -
ตัวอย่าง
<?php
$my_array = array("This", 91, '', null, 102, "is", false, "a", "sample", null);
foreach($my_array as $key => $val)
if(empty($val))
unset($my_array[$key]);
echo "After removing null values from the array, the array has the below elements -";
foreach($my_array as $key => $val)
echo ($my_array[$key] ."<br>");
?> ผลลัพธ์
After removing null values from the array, the array has the below elements -This 91 102 is a sample
อาร์เรย์ถูกกำหนด ซึ่งประกอบด้วยสตริง ตัวเลข และค่า 'null' วนรอบ 'foreach' ใช้เพื่อวนซ้ำองค์ประกอบ และหากค่าว่างเปล่า นั่นคือมีค่าว่าง ค่านั้นจะถูกลบออกจากอาร์เรย์ อาร์เรย์ที่เกี่ยวข้องจะปรากฏขึ้นอีกครั้งซึ่งไม่มีค่าว่าง