dimanche 26 avril 2015

print random result in php 2 dimensional array

for a simple array I can print a random item with this code:

$key = array_rand($arr);
$value = $arr[$key];

But I need do it with 2 dimensional array and one condition.

I have an array like this

$arr = array ( 'news' => 'text1',
               'news' => 'text2',
               'fun'  => 'text3',
               'news' => 'text4',
               'echo' => 'text5',
               'fun' => 'text6');

I want somthing like this algorithm in php

if($type == 'news')
   print random($arr($type));

So the results are:

text1 or text2 or text4

or another example:

 if($type == 'fun')
       print random($arr($type));

results: text3 or text6

but how can I do it?




Aucun commentaire:

Enregistrer un commentaire