我有一个场景:
有些div正在显示基于以下循环:
<?php foreach($posts->content as $entry) { ?>
<div><a class="popup-with-zoom-anim wiplay" id="<?=$entry->id?>"
href="#small-dialog" data-detail-id ="<?=$entry->id?>"
data-stream="******">content here</a></div>
<?php } ?>
在a href&#34;数据流&#34;我想传递一些youtube网址,所以每个div将显示基于for循环的随机网址。 我尝试了以下代码。但是没有工作。我们如何在循环中调用此函数?
$assoc_array = array( "url" => "https://www.youtube.com
/watch?v=A7XdOyZIkko",
"url" => "https://www.youtube.com/watch?v=dMH0bHeiRNg
",
"url" => "https://www.youtube.com/watch?v=xEs59zTXu7s",
"url" => "https://www.youtube.com/watch?v=tlDAgZO2ZDM
");
function shuffle_assoc_array(&$array) {
if (!is_array($array)) return $array;
$keys = array_keys($array);
shuffle($keys);
$random = array();
foreach ($keys as $key)
$random[$key] = $array[$key];
$array = $random; return TRUE;
}
shuffle_assoc_array($assoc_array);
如何将此youtube网址随机传递给for循环?
答案 0 :(得分:0)
这个数组只给你一个值
array(
"url" => "https://www.youtube.com
/watch?v=A7XdOyZIkko",
"url" => "https://www.youtube.com/watch?v=dMH0bHeiRNg",
"url" => "https://www.youtube.com/watch?v=xEs59zTXu7s",
"url" => "https://www.youtube.com/watch?v=tlDAgZO2ZDM
");
这相当于
array(
"url" => "https://www.youtube.com/watch?v=tlDAgZO2ZDM
");
尝试这样的事情
array(
"https://www.youtube.com/watch?v=A7XdOyZIkko",
"https://www.youtube.com/watch?v=dMH0bHeiRNg",
"https://www.youtube.com/watch?v=xEs59zTXu7s",
"https://www.youtube.com/watch?v=tlDAgZO2ZDM"
);
答案 1 :(得分:0)
正如Ima所说,你必须要改变你的数组格式,而不是尝试下面的代码。
<?php
$assoc_array = array( "https://www.youtube.com/watch?v=A7XdOyZIkko", "https://www.youtube.com/watch?v=dMH0bHeiRNg", "https://www.youtube.com/watch?v=xEs59zTXu7s", "https://www.youtube.com/watch?v=tlDAgZO2ZDM");
shuffle($assoc_array);
for($i=0;$i<count($assoc_array);$i++){
echo '<div><a class="popup-with-zoom-anim wiplay" id="'.$i.'" href="#small-dialog" data-detail-id ="'.$i.'" data-stream="'.$assoc_array[$i].'">'.$assoc_array[$i].'</a></div>';
}
?>