PHP程序中随机获取一句话:打乱数组次序,再取数组中第一条数据


打乱数组次序,再取数组中第一条数据,即可达到随机获取一句话
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="http://blog.1500km.com/sites/ui-blog/other/favicon.ico" rel="shortcut icon" />
<link href="http://blog.1500km.com/sites/ui-blog/other/favicon.ico" rel=bookmark />
<?php 
//打乱数组次序,再取数组中第一条数据,即可达到随机获取一句话之目的
$x = rand_say();
shuffle($x);
?>
<style>
body{ font-size:12px; color:#666666; }
hr{ border-bottom:dashed 1px #ccc; border-top:solid 0px #ccc;}
.btn{ background-color:#339933; color:#FFFFFF; padding:3px;}
</style>
<form action="" target="_self">
<img src="http://blog.1500km.com/sites/download/images/1500km.png" style="vertical-align:middle;" />
<input type="submit" value="点击这里随机获取一句话" class="btn">
</form><hr>
<?=$x[0][0]?><hr>
<?=$x[0][1]?><hr>
<?
function rand_say(){
return array(
array("God gives answers in three ways: He says yes and gives you what you want. He says no and gives you something better. He says wait and gives you the best. "
,"上帝用三种方式作答:他点头给你想要的;他摇头给你更好的;他让你等,就给你最棒的。")

,array("Do not pray for easy lives, Pray to be stronger men.  "
,"不要祈祷生活的舒适,祈祷自己变得更坚强。")

,array("Do what you say, say what you do . "
,"做你说过的,说你能做的。")

,array("Don t set your goals by what other people deem important. Only you know what is best for you.  "
,"别人认为重要的, 并不就是你的追求。只有自己才知道什么最适合自己。")

,array("If you want to say `no`, say it. `Maybe` is for people who are afraid to commit.  "
,"想说不,就大胆说出来,什么也许,什么可能,那些都是留给害怕承担的人去说的。")

,array("Time is the deposit each one has in the bank of God and no one knows the balance. "
,"时间是我们在上帝银行的存款,只是没有人知道还有多少结余。")

,array("Nobody can go back and start a new begining, but anyone can start today and make a new ending. "
,"没有人可以回到过去重新开始,但谁都可以从今日开始,书写一个全然不同的结局。")

,array("Courage is the discovery that you may not win, and trying when you know you can lose."
,"勇气, 是当你发现没有赢的把握,明知会输,却不放弃。")

,array("Whatever with the past has gone, the best is always yet to come. "
,"无论过去发生什么,最好的尚未到来。")

,array("I am not afraid of tomorrow, for I have seen yesterday and love today. "
,"我不害怕明天,因为我经历过昨天,又热爱今天。")

,array("What other people think of you is none of your business. "
,"别人爱怎么想那是他们的事,你就是你。")

,array("If you don't go after what you want, you'll never have it. If you don't ask, the answer is always no. If you don't step forward, you're always in the same place. "
,"不去追逐你所渴求,你将永远不会拥有。不开口问,回答永远是No。不往前走,就将永远停留。")

,array("They say a person needs just 3 things to be truly happy in this world, someone to love, something to do, and something to hope for. "
,"人生拥有三件事便可真正快乐:有爱的人,有喜欢的事业,有梦想。")

,array("Do not anticipate trouble, or worry about what may never happen. Keep in the sunlight. "
,"不要去预见烦恼或担心可能永远不会发生的事情。置身于明媚的阳光之中吧。")

,array("Remember what should be remembered, and forget what should be forgotten. Alter what is changeable, and accept what is mutable."
,"记住该记住的,忘记该忘记的。改变能改变的,接受不能改变的。")

,array("When life gives you a hundred reasons to cry, show life that you have a thound reasons to smile."
,"当生活给你100伤心的原因,你就还它1000个微笑的理由。")

,array("If you want a happy life, tie it to a goal, not to people or things。"
,"如果你想过得快乐,把生活跟目标联系在一起,而不是跟某个人或某些事。")

,array("A certain amount of care or pain or trouble is necessary for every man at all times .A ship without a ballast is unstable and will not go straight. "
,"一定的忧愁、痛苦或烦恼,对每个人都是时时必需的一艘船如果没有压舱物,便不会稳定,不能朝着目的地一直前进。")

,array("The world makes way for the man who knows where he is going. "
,"只要你明确了自己的方向,世界也会为你让路。")

,array("It takes courage to follow your mind. But it takes everything to follow your heart. "
,"跟着理智走,要有勇气;跟着感觉走,要倾其所有的决心。")

,array("The things that we persistently chase after now, will become unimportant someday. "
,"现在执着追求的事,将来必定有一天变成不重要的。 ")

,array("No matter how serious life gets, you still gotta have that one person you can be completely stupid with."
,"无论生活变得多么严肃,你总会找到一个让你心甘情愿傻傻相伴的人。")

,array("You are a part of a puzzle 0f someone’s life. You may never kn0w where you fit, but always remember that someone’s life may not be complete without you! "
,"你是某人生命拼图中的一小块,也许你永远都不知道自己的位置,但请记住:没有你,他的生命是不完整的。")

,array("It is better to stay silent and be thought a fool, than to open one’s mouth and remove all doubt."
,"宁愿保持沉默让人看起来像个傻子,也不要一开口就证明自己确实如此。")

,array("Commitment doesn’t mean sticking to a person when you don’t have any option, it means keeping a relation with someone even though you ve lots of options. "
,"承诺,并不是你除了他别无选择,而是即使你拥有了整座森林,依然选择了他。")

,array("Do not blame your food because you have no appetite."
,"不要因为自己没胃口,就责备(无辜的)食物。")

,array("Things may come to those who wait, but only the things left by those who hustle."
,"守株待兔或许也会有所得,但得到的总是别人争取后剩下的东西。")

,array("However mean your life is, meet it and live it; do not shun it and call it hard names. "
,"不论你的生活如何卑贱,你要面对它生活,不要躲避它,更别用恶言咒骂它。")

,array("If a woman is not sexy, she needs emotion; if not emotional, she needs reason; if not reasonable, she has to know herself clearly. If not so, only she has is misfortune."
,"女人如果不性感,就要感性;如果没有感性,就要理性;如果没有理性,就要有自知之明;如果连这个都没有了,她只有不幸.")

,array("Dance like there’s nobody watching; Love like you’ll never be hurt; Work like you don't need money; And live like it’s heaven on earth."
,"跳舞吧,就像没有人注视一样;去爱吧,就像从未受伤一样;工作吧,就像不需要金钱一样; 生活吧,就像今日是末日一样。")

,array("You can always be a worse version of `him`, or better version of yourself."
,"不是要做一个单纯优秀的人,而是要做一个不可替代的人。")

,array("Time can prove love, but also be able to love overturned."
,"时间能够证明爱情,也能够把爱推翻。")

,array("Sometimes you have to smile and act like everything is okay, hold back the tears and walk away. "
,"有时候你不得不笑,表现得好像一切OK,然后,忍着眼泪走开。")

,array("It is kind of fun to do the impossible. "
,"明知不可为而为之是一种乐趣。")

,array("If you want to make your dream come true ,the first thing you have to do is to wake. "
,"如果想梦想成真,第一件要做的事就是醒来。")

,array("A real friend is one who walks in when the rest of the world walks out."
," 真正的朋友会在整个世界都离你远去的时候,仍然与你并肩。")

,array("No matter when U start, you do not stop after starting. No matter when U end, you do not regret after ended."
,"不论什么时候开始,重要的是开始之后就不要停止。不论什么时候结束,重要的是结束之后就不要悔恨。")

,array("Never expect the perfect man, it's not because that you cannot find, but just because there is no perfect man. "
,"不要期待完美的男人,不是因为你期待不到,而是根本没有完美的男人。")

,array("Anger always comes from frustrated expectations. "
,"生气永远来自落空的期待。")

,array("No matter how serious life gets, you still gotta have that one person you can be completely stupid with. "
,"无论生活得多么艰难,最后你总会找到一个让你心甘情愿傻傻相伴的人。")

,array("Apologizing does not always mean that you are wrong and the other person is right, it just means you value your relationship more than your ego. "
,"道歉并不总是代表我承认自己错了,只能说,我在乎我们的关系,比我自身还在乎。")

,array("In this world, only those men who really feel happy can give women happiness."
,"在这个世界上,只有真正快乐的男人,才能带给女人真正的快乐。")

,array("No matter how much we think we know, we never know everything. Stay positive, stop judging! "
,"不管我们认为自己多了解,也永远不可能了解一切。保持乐观,别再评判了!")

,array("Remember everything happens for a reason. If it changes your life. Let it. "
,"记住:任何事情发生,都有它的道理。如果它改变了你的生活,接受它。")

,array("Don't wish it were easier, wish you were better."
,"不要寄希望于事情变得简单,还是期待自己能做得更好吧。")

,array("Save your heart for someone who cares. "
,"把心留给在乎你的人。")

,array("Why should a lifetime to forget someone, because you do not try to forget, but always remember, in looking forward, in the dream. "
,"忘记一个人为什么要一辈子,因为你根本没有试着去忘记,而是一直在怀念,在期待,在做梦。")

,array("I know someone in the world is waiting for me, although I've no idea of who he is. But I feel happy every day for this. "
,"我知道这世上有人在等我,虽然我不知道他是谁,但为了这个,我每天都非常快乐。")

,array("The truth may hurt for a little while but a lie hurts forever. "
,"真相会让我们痛一阵,但谎言令我们痛一生。")

,array("If you love someone, tell them. Hearts are often broken by words left unspoken. "
,"如果爱上谁,告诉他们。心会碎掉的原因,有时候是因为自己的一些话一直开不了口。")

);
}

?>
<br>
<br>
<a target="_blank" href="http://www.1500km.com">返回1500km</a>

程序运行结果如下图所示,点击这里亲自运行看看