Home >Backend Development >PHP Tutorial >PHP method to dynamically create html code based on array
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('Cat','Mat','Fat','Hat'); //Array contents array 2 :: key => value $myArray2 = array('c'=>'Cat','m'=>'Mat','f'=>'Fat','h'=>'Hat'); //Values from array 1 echo'<select name="Words">'; //for each value of the array assign a variable name word foreach($myArray1 as $word){ echo'<option value="'.$word.'">'.$word.'</option>'; } echo'</select>'; //Values from array 2 echo'<select name="Words">'; //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'<option value="'.$let.'">'.$word.'</option>'; } echo'</select>'; ?>
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!