下面小编就为大家带来一篇php 数组随机取值的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
array_rand() 在你想从数组中取出一个或多个随机的单元时相当有用。它接受 input 作为输入数组和一个可选的参数 num_req,指明了你想取出多少个单元 - 如果没有指定,默认为 1。
array_rand -- 从数组中随机取出一个或多个单元
mixed array_rand ( array input [, int num_req])
array_rand() 在你想从数组中取出一个或多个随机的单元时相当有用。它接受 input 作为输入数组和一个可选的参数 num_req,指明了你想取出多少个单元 - 如果没有指定,默认为 1。
如果你只取出一个,array_rand() 返回一个随机单元的键名,否则就返回一个包含随机键名的数组。这样你就可以随机从数组中取出键名和值。
不要忘记调用 srand() 来撒下随机数发生器的种子。
例子 1. array_rand() 例子
srand ((float) microtime() * 10000000); $input = array ("Neo", "Morpheus", "Trinity", "Cypher", "Tank"); $rand_keys = array_rand ($input, 2); print $input[$rand_keys[0]]."\n"; print $input[$rand_keys[1]]."\n";
我们曾经访问过这样的网站,每次刷新banner都随机的变化,在这篇文章中,我们将给大家介绍用PHP来实现这个功能。
步骤
程序实现的原理是:调用一个数组,每个图象对应一个数组中的元素,然后我们设置随机数,只要随机得到一个数据就可以显示一副图象了。
第一个步是我们来产生一个随机数。每次刷新时我们都得到不同的随机数,具体代码为:
srand((float) microtime() * 10000000);
之后我们设置一个数组为image,然后再设置5个数组元素,代码如下:
$image[1]='/location/of/image1.jpg'; $image[2]='/location/of/image2.jpg'; $image[3]='/location/of/image3.jpg'; $image[4]='/location/of/image4.jpg'; $image[5]='/location/of/image5.jpg';
下面的代码实现的功能是从数组中随机选择一个元素:
$rn = array_rand($image);
然后我们来显示一个随机的图片:
echo '<img src="'.$image[$rn].'">';
把上面的代码组合起来就可以了。
srand((float) microtime() * 10000000); $image[1]='/location/of/image1.jpg'; $image[2]='/location/of/image2.jpg'; $image[3]='/location/of/image3.jpg'; $image[4]='/location/of/image4.jpg'; $image[5]='/location/of/image5.jpg'; $rn = array_rand($image); echo '<img src="'.$image[$rn].'">';
以上的代码是我们随机显示图片的代码,如果我们想使每个图片再加上各自的连接地址那么我们把上述的代码稍微改动下就可以了!把上述的数组改为二维数组:
$image[1]['pic']='/location/of/image1.jpg'; $image[1]['link']='/location/of/link1.php';
相应的显示代码为:
echo '<a href="'.$image[$rn]['link'].'">'; echo '<img src="'.$image[$rn]['pic'].'">';
那么我们就可以完成我们标题的功能了,随机显示图片并且连接到不同的指定的地址:
srand((float) microtime() * 10000000); $image[1]['pic']='/location/of/image1.jpg'; $image[1]['link']='/location/of/link1.php'; $image[2]['pic']='/location/of/image2.jpg'; $image[2]['link']='/location/of/link2.php'; $image[3]['pic']='/location/of/image3.jpg'; $image[3]['link']='/location/of/link3.php'; $image[4]['pic']='/location/of/image4.jpg'; $image[4]['link']='/location/of/link4.php'; $image[5]['pic']='/location/of/image5.jpg'; $image[5]['link']='/location/of/link5.php'; $rn = array_rand($image); echo '<a href="'.$image[$rn]['link'].'">'; echo '<img src="'.$image[$rn]['pic'].'">';
你可以把上面的代码拷到你的网页中去运行了。祝你好运
更多相关教程请访问 php编程从入门到精通全套视频教程

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.