我正在使用以下教程来学习如何在PHP中进行随机播放,但输出很难看。如何更改shuffle的输出更符合我想以列表格式显示的轮询?
我正在使用以下教程来学习如何执行此操作:
http://www.w3schools.com/php/func_array_shuffle.asp
<?php
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
shuffle($my_array);
print_r($my_array);
?
但输出很难看:数组([0] =&gt; Cat [1] =&gt; Dog [2] =&gt;马)
如何使shuffle数组看起来很漂亮?
<ul>
<li><input id="pollRadioButton1" name="pollAnswerID" type="radio" value="1" /> Answer1 for Poll1</li>
<li class="pollChart pollChart1"> </li>
<li><input id="pollRadioButton2" name="pollAnswerID" type="radio" value="2" /> Answer2 for Poll1</li>
<li class="pollChart pollChart2"> </li>
</ul>
答案 0 :(得分:2)
<?php
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
shuffle($my_array);
?>
<ul>
<?php foreach ($my_array as $key => $element): ?>
<li>
Whatever you want to do with
<?php echo htmlspecialchars($element) ?>
(<?php echo htmlspecialchars($key) ?>).
</li>
<li class="pollChart pollChart1"> </li>
<?php endforeach ?>
</ul>
答案 1 :(得分:-1)
print_r()
仅用于调试。
要获得“干净”输出,您必须指定 应该如何输出:
<?php
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
shuffle($my_array);
foreach ($my_array as $key=>$val)
echo sprintf('%s: %s<br>'."\n",$key,$val);
?>
在您的情况下,输出可能类似于:
echo sprintf('<li><input id="pollRadioButton[]" name="%1$s" '.
'type="radio" value="%2$s" />%2$s</li>'."\n".
'<li class="pollChart pollChart1"> </li>'."\n",$key,$val);