March 3, 2013

php array to radio button

Question by user2128593

i want to create a radio button labeled with the values of my array. all i can do is to print them all. what can i use to access my array (dynamic-array) besides indexes since i will not know the order and number of files inside my languages directory? Thanks!

input

english.xml,mandarin.xml,french.xml

these files is saved at languages and i will use the file names as labels in my radio button form.

$files = glob("languages/*.xml");

foreach($files as $file){

   $file = substr($file, 10); //removes "languages/"
   $file = substr_replace($file, "", -4); //removes ".xml"
   ?>

   <p><?=$file?></p> // prints out the filename
   <?}?>

output

<form action="">
<input type="radio" name="lang" value="english">english
<input type="radio" name="lang" value="mandarin">mandarin
<input type="radio" name="lang" value="french">french
</form>

sorry for my bad english i hope i explained it well.

Answer by Starx

You can access the key of the array using foreach too. Like this:

foreach($files as $key => $value) {
    //....
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!