Home  >  Article  >  Backend Development  >  php how to output array using loop and separated by blank line

php how to output array using loop and separated by blank line

PHPz
PHPzOriginal
2023-04-23 10:22:451329browse

In PHP, there are many ways to output an array, and using a loop is the most common method. In some cases, we may need to add a line of space between each element while outputting the array elements to increase readability. This article will introduce how to use a loop to output an array in PHP and add a line of spaces between each element.

1. Use for loop to output array

Using for loop to output array is the most basic method. You can use the count() function to get the array length, then loop through the array elements and output each element. To add a line of space between each element, you need to use the echo "\n" statement to output a newline character after outputting each element. The following is a sample code:

$array = array("apple", "banana", "orange", "pear");
$length = count($array);
for ($i = 0; $i < $length; $i++) {
    echo $array[$i];
    echo "\n";
}

Output result:

apple
banana
orange
pear

2. Use foreach loop to output array

Using foreach loop to output array is a more common method. This method can automatically obtain the key value and element value of the array without manually calculating the array length. Similarly, we can use echo "\n" to output a newline character after each element to add a line of space between elements. The following is a sample code:

$array = array("apple", "banana", "orange", "pear");
foreach ($array as $value) {
    echo $value;
    echo "\n";
}

Output result:

apple
banana
orange
pear

3. Use the implode function to output the array

Use the implode function to splice the array elements together and use a Strings are concatenated. We can add a newline character when concatenating strings to add a line of space between each element. The following is the sample code:

$array = array("apple", "banana", "orange", "pear");
echo implode("\n", $array);

Output result:

apple
banana
orange
pear

Summary:

In PHP, by looping through the output array and adding a line of spaces between each element, there is There are many methods to choose from. We can use a basic for loop and count() function, or use a foreach loop to automatically get the array key values ​​and element values, or we can use the implode function to concatenate the array elements into a string and add a newline character when concatenating. Either way, you can add a line of space between array elements to make the output clearer and more readable.

The above is the detailed content of php how to output array using loop and separated by blank line. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn