schoolteacher schoolteacher - 3 months ago 14
PHP Question

How to show 3 items one time - array

$urls = array("http://google.com" ,"http://hotmail.com" ,"http://yahoo.com" ,"http://stackoverflow.com" ,"http://bing.com" ,"http://cnn.com" );

$text = array("Google" ,"Hotmail" ,"Yahoo" ,"Stackoverflow" ,"Bing" ,"CNN");
srand(time());

$random = (rand()%3);
echo ("<a href = \"$urls[$random]\">$text[$random]</a>");


Above will do this:

<a href="http://www.stackoverflow.com">Stackoverflow</a>


How to show 3 links for example Yahoo, Google, Bing

Answer

You can reference the same element of the array. A function should suffice for this

function showLink(Array $url, Array $title, $position) {
    return '<a href = "' . $url[$position] . '">' . $title[$position] . '</a>';
}

echo $showLink($urls, $text, mt_rand(0, count($urls) -1);
Comments