Home > Backend Development > PHP Tutorial > PHP method to dynamically create html code based on array

PHP method to dynamically create html code based on array

墨辰丷
Release: 2023-03-30 20:44:01
Original
1885 people have browsed it

This article mainly introduces the method of dynamically creating html code based on arrays in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

The example of this article describes the method of generating an html drop-down list from a php array, as follows:

This code can dynamically generate an html drop-down list (select) based on the defined php array

<?php 
//Array contents array 1 :: value 
$myArray1 = array(&#39;Cat&#39;,&#39;Mat&#39;,&#39;Fat&#39;,&#39;Hat&#39;); 
//Array contents array 2 :: key => value 
$myArray2 = array(&#39;c&#39;=>&#39;Cat&#39;,&#39;m&#39;=>&#39;Mat&#39;,&#39;f&#39;=>&#39;Fat&#39;,&#39;h&#39;=>&#39;Hat&#39;); 
//Values from array 1 
echo&#39;<select name="Words">&#39;; 
//for each value of the array assign a variable name word 
foreach($myArray1 as $word){ 
  echo&#39;<option value="&#39;.$word.&#39;">&#39;.$word.&#39;</option>&#39;; 
} 
echo&#39;</select>&#39;; 
//Values from array 2 
echo&#39;<select name="Words">&#39;; 
//for each key of the array assign a variable name let 
//for each value of the array assign a variable name word 
foreach($myArray2 as $let=>$word){ 
  echo&#39;<option value="&#39;.$let.&#39;">&#39;.$word.&#39;</option>&#39;; 
} 
echo&#39;</select>&#39;; 
?>
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

How to set the page timeout in php

How to create a Session in Php

Three ways to generate random strings in PHP

The above is the detailed content of PHP method to dynamically create html code based on array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template