How to get elements in the array that contain data in PHP – list() and each() function

We can use list() and each() function to return only the elements in the array that contain data. With the help of list() and each() functions, we can use entire contents of an array with minimum efforts.
Suppose you want to display the each element in the array that contain data. see the following example:

Array of the capitals:

$Capitals[3] = "New Delhi";
$Capitals[7] = "Washington";
$Capitals[1] = "Landon";
$Capitals[5] = "Paris";
$Capitals[11] = "Moscow";
$Capitals[] = "Tokyo";

Now you can write the following code snippet to print the name of the each capital name in the array:

while(list($element_index,$element_content)=each($Capitals))
{
 echo "<br>$element_index - $element_content";
}

here $element_index indicates the element index and $element_content indicates the element content.
Result will be:
3 – New Delhi
7 – Washington
1 – Landon
5 – Paris
11 – Moscow
12 – Tokyo

suppose you want to use only content of the elements then you can use following code snippet:

while(list(,$element_content)=each($Capitals))
{
 echo "<br> $element_content";
}

Result will be:
New Delhi
Washington
Landon
Paris
Moscow
Tokyo

It is need to notice that list() function returns the index value first and then element content.