Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php rand()洗牌xml输出_Php_Xml_Random_Shuffle - Fatal编程技术网

Php rand()洗牌xml输出

Php rand()洗牌xml输出,php,xml,random,shuffle,Php,Xml,Random,Shuffle,我创建了一个包含许多项的XML文件,如下例所示: <?xml version="1.0" encoding="UTF-8"?> <feed xmlns="http://www.w3.org/2005/Atom"> <id>http://www.example.com/index.php?pagina=video-categories</id> <title><![CDATA[Example.com Categories R

我创建了一个包含许多项的XML文件,如下例所示:

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <id>http://www.example.com/index.php?pagina=video-categories</id>
  <title><![CDATA[Example.com Categories RSS]]></title>
  <author>
  <name>Example.com</name>
  <email>email@example.com</email>
  </author>
  <updated></updated>
  <link rel="alternate" href="http://www.example.com/categories"/>
  <subtitle><![CDATA[HQ example videos in any category]]></subtitle>
  <rights>Copyrights reserved. Feel free to use the embed function.</rights>
    <item>
    <categoryname><![CDATA[100 Latest Videos]]></categoryname>
    <shortcategoryname><![CDATA[100 New Sex Clips]]></shortcategoryname>
    <categoryurl>http://www.example.com/1.html</categoryurl>
    <categoryimage>http://www.example.com/12347.jpg</categoryimage>
    </item>
    <item>
    <categoryname><![CDATA[100 Latest Videos]]></categoryname>
    <shortcategoryname><![CDATA[100 New Sex Clips]]></shortcategoryname>
    <categoryurl>http://www.example.com/2.html</categoryurl>
    <categoryimage>http://www.example.com/12346.jpg</categoryimage>
    </item>
    <item>
    <categoryname><![CDATA[100 Latest Videos]]></categoryname>
    <shortcategoryname><![CDATA[100 New Sex Clips]]></shortcategoryname>
    <categoryurl>http://www.example.com/3.html</categoryurl>
    <categoryimage>http://www.example.com/12345.jpg</categoryimage>
    </item>
    ... and more items ...
    </feed>

http://www.example.com/index.php?pagina=video-类别
';
}
echo$html;
?>
我只想显示6个链接,并从XML提要中将它们的位置设置为radom。我希望有来自xml项目的随机链接。我应该添加或更改什么?我应该使用
rand()
shuffle()
来回显6个随机链接吗


php代码是我现在用来回显一些链接的代码,但它不是随机的…

$xml->item
是一个数组,所以
将其洗牌,然后获得第一个
6
,类似于:

shuffle($xml->item);

foreach(array_slice($xml->item, 0, 6) as $item) {
    $categoryname = $item->categoryname;
    $shortcategoryname = $item->shortcategoryname;
    $categoryurl = $item->categoryurl;
    $html .= '<a class="purplewidebutton" href="' . $categoryurl . '" title="' . $categoryname . '">' . $shortcategoryname . '</a>';
}
echo $html;

你的PHP是什么?我在问题中使用了显示的PHP代码,但xml文件url只是一个示例url,而不是真正的url。抱歉,我没有滚动。我更改了问题以使其更可读,我很抱歉没有将PHP添加到自己的代码块中,但编辑后不滚动即可阅读,谢谢。非常感谢,它的工作非常好,我会在你的答案中使用第二个选项,再次感谢你的答案
foreach
将更简单、更通用,例如,如果向XML中添加项,则无需更改循环$shortcategoryname=$xml->item[$i]->shortcategoryname$categoryurl=$xml->item[$i]->categoryurl$html[]='';}洗牌($html);回声内爆(“\n”,数组_切片($html,0,6));?>如果我在xml提要中删除/添加类别,我只需要更改35个数字,在我使用缓存的页面上,所以我一页只运行4次/day@AbraCadaver这太棒了。我最初也使用了
for
循环,因为我听说它更快。有没有办法用
for
代替
foreach
shuffle($xml->item);

foreach(array_slice($xml->item, 0, 6) as $item) {
    $categoryname = $item->categoryname;
    $shortcategoryname = $item->shortcategoryname;
    $categoryurl = $item->categoryurl;
    $html .= '<a class="purplewidebutton" href="' . $categoryurl . '" title="' . $categoryname . '">' . $shortcategoryname . '</a>';
}
echo $html;
$html[] = '<a class="purplewidebutton" href="' . $categoryurl . '" title="' . $categoryname . '">' . $shortcategoryname . '</a>';
shuffle($html);
echo implode("\n", array_slice($html, 0, 6));