user2669688
user2669688

Reputation: 3

php random link from array

<? 
    $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>'
?>

doesn't seem to work trying to get it to display one of those links randomly every click..

Upvotes: 0

Views: 3580

Answers (3)

Emilio Gort
Emilio Gort

Reputation: 3470

change this

 $thelink = $randomlink[0];

to

$thelink = $urls[$randomlink];

$randomlink = array_rand($urls, 1)

Picks one or more random entries out of an array, and returns the key (or keys) of the random entries

Upvotes: 0

user2062950
user2062950

Reputation:

array_rand returns the keys of the entries, not the entires themselves.

Here's an example from the 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";
?>

So in your case you probably want $thelink = $urls[$randomlink[0]];

Upvotes: 0

Chris Rockwell
Chris Rockwell

Reputation: 1842

$randomlink contains the array key, so you would use: $thelink = $urls[$randomlink]. See array_rand() on php.net

Upvotes: 1

Related Questions