从数组的PHP随机链接

时间:2013-08-10 02:53:52

标签: php

<? 
    $urls = array(
    array(
        'http://cur.lv/xlnc',
        'http://cur.lv/xln8',
        'http://cur.lv/xln5',
        'http://cur.lv/xln4',
        'http://cur.lv/xlmv',
        'http://cur.lv/xlms',
        'http://cur.lv/xllz',
        'http://cur.lv/xllp',
        'http://cur.lv/xllj',
        'http://cur.lv/xlle',
        'http://cur.lv/xll9',
        'http://cur.lv/xll5',
        'http://cur.lv/xlks',
        'http://cur.lv/xlkl',
        'http://cur.lv/xlke',
        'http://cur.lv/xlk4',
        'http://cur.lv/xljv',
        'http://cur.lv/xlje',
        'http://cur.lv/xlj9',
        'http://cur.lv/xlj1',
        'http://cur.lv/xjxu',
        'http://cur.lv/xjxd',
        'http://cur.lv/xjx4',
        'http://cur.lv/xjwz',
        'http://cur.lv/xjw1',
        'http://cur.lv/xjup',
        'http://cur.lv/xjtz',
        'http://cur.lv/xjtt',
        'http://cur.lv/xjtn',
        'http://cur.lv/xjrh',
        'http://cur.lv/xjrd',
        'http://cur.lv/xjr3',
        'http://cur.lv/xj1z',
        'http://cur.lv/xizx',
        'http://cur.lv/xizf',
        'http://cur.lv/x3jx',
        'http://cur.lv/x3jp'
    )
);

$randomlink = array_rand($urls, 1);
$thelink = $randomlink[0];
echo '<a target="_blank" href="' . $thelink . '">Random Faucet</a>'
?>

似乎无法尝试让它在每次点击时随机显示其中一个链接..

3 个答案:

答案 0 :(得分:1)

$randomlink包含数组键,因此您将使用:$thelink = $urls[$randomlink]。见array_rand() on php.net

答案 1 :(得分:0)

array_rand会返回条目的,而不是条目本身。

以下是http://php.net/manual/en/function.array-rand.php

的示例
<?php
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";
?>

所以在你的情况下你可能想要$thelink = $urls[$randomlink[0]];

答案 2 :(得分:0)

更改此

 $thelink = $randomlink[0];

$thelink = $urls[$randomlink];

$randomlink = array_rand($urls, 1)

从数组中挑选一个或多个随机条目,并返回随机条目的键(或多个键)