【发布时间】:2015-07-05 03:47:45
【问题描述】:
对于一个简单的数组,我可以使用以下代码打印一个随机项:
$key = array_rand($arr);
$value = $arr[$key];
但我需要使用二维数组和一个条件来完成。
我有一个这样的数组
$arr = array ( 'news' => 'text1',
'news' => 'text2',
'fun' => 'text3',
'news' => 'text4',
'echo' => 'text5',
'fun' => 'text6');
我想在 php 中使用类似这种算法的东西
if($type == 'news')
print random($arr($type));
所以结果是:
text1 或 text2 或 text4
或另一个例子:
if($type == 'fun')
print random($arr($type));
结果:text3 或 text6
但是我该怎么做呢?
【问题讨论】:
-
你不能有多个同名的键!密钥必须是唯一的!