$images = array("image1.jpg", "image2.jpg", "image3.jpg");
<img>
tag. Here is the sample code:
foreach($images as $image) { echo '<img src="' . $image . '">'; }
$images
is the array we want to iterate through. $image
is the file name of each image in the loop. The echo
statement is used to output the complete HTML code of the <img>
tag to the browser. The src
attribute specifies the path to the image to be displayed.
<p>If we wanted to display a title or description for each image, we could store this information in an associative array and display it next to the image as we iterate over the array. Here is the sample code:
$images = array( array("image1.jpg", "美丽的沙滩"), array("image2.jpg", "美味的食物"), array("image3.jpg", "迷人的风景"), ); foreach($images as $image) { echo '<div>'; echo '<img src="' . $image[0] . '">'; echo '<p>' . $image[1] . '</p>'; echo '</div>'; }
$image[0]
to get the image file name and $image[1]
to get the image description. In HTML, we use the <div>
element to draw the container for each image and title, and the <p>
element to display the title.
<p>In short, it is very simple to traverse and display an image array using PHP, just use a simple foreach loop and the HTML img element. If you also need to display a title or description, then an associative array is a good choice. Hope this article can be helpful to you, thank you! The above is the detailed content of How to display array images in php traversal. For more information, please follow other related articles on the PHP Chinese website!