How can I use a php array as the value of an HTML <option>
?
eg
<select name='myname'>
<option value=' array("font-family" => "font-family: 'Yeseva One', serif","font-name" => "Yeseva One","css-name" =>"Yeseva+One")'>
Font 1
</option>
...
</select>
It kind of depends on what you want to archive in the end. I've got 3 options for you:
1) Json is pretty flexible:
<option value="<?php json_encode($yourArray) ?>">Font 1</option
You can then later on convert Json back to an array with json_decode.
2) If you need the data for server client side scripting, it would probably be a better idea to use HTML5's data attributes:
<option value="value1" data-fontname="Yeseva One" data-cssname="Yeseva+One">Font 1</option>
3) You can use hidden input fields, which will allow you to retrieve the values like $_POST['font1']['css_name'] ect. :
<input type="hidden" name="font1[font_name]" value="Yeseva One" />
You will obviously have to escape your values. But you get the idea.
I think the best approach for you is to put the values with comma separation ie
<select name='myname'>
<option value='Yeseva One, serif'</option>
</select>
and in the php you can implode the result into an array
$array_of_results = implode( $_POST['myname'] );
I think you can serialize the array:
<?php
$arrYourArray = array(
"font-family" => "font-family: 'Yeseva One', serif",
"font-name" => "Yeseva One",
"css-name" =>"Yeseva+One");
?>
<select name="myname">
<option value="<?php echo serialize($arrYourArray); ?> ">
Font 1
</option>
...
</select>
Are you trying to create a select form based on the options you have set in your array?
If so, you could do this:
<select name="myname">
<?php
$selects = Array('Verdana' => 'font-family: Verdana;', '\'Trebuchet MS\', Helvetica, Arial, sans-serif' => 'font-family: \'Trebuchet MS\', Helvetica, Arial, sans-serif;');
foreach($selects as $select => $css) {
echo '<option value="' . $css . '">' . $select . '</option>';
}
?>
</select>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.