Home > Backend Development > PHP Tutorial > PHP多个数组遍历后的字符串加CSS格式后再随机输出。该怎么解决

PHP多个数组遍历后的字符串加CSS格式后再随机输出。该怎么解决

WBOY
Release: 2016-06-13 10:11:35
Original
1046 people have browsed it

PHP多个数组遍历后的字符串加CSS格式后再随机输出。
$a='长颈鹿,大象,小猫,老虎,';/*此段字符颜色红色,字号12*/

$b='兔子,猫肉,羊肉,牛肉,';/*此段字符颜色紫色,字号16*/

$c='你好,谢谢,再见,很好,';/*此段字符颜色黄色,字号20*/

/*三段字符串,分别与中间逗号分割。用以下一步的分割成数组*/

$aa=explode(",",$a.$b.$c);
/*数组后,然后我想的是用array_rand()函数,然后遍历出随机排列的字符串*/

现在的问题是,如何将$a $b $c,这三组字符串分别设定好CSS颜色,文字大小。然后汇总在一起,再随机排列输出。


------解决方案--------------------
再试试这个,看看效果
header('content-type:text/html; charset=utf-8');

$arr = array('长颈鹿', '大象', '小猫', '老虎', '兔子', '猫肉', '羊肉', '牛肉', '你好', '谢谢', '再见', '很好');
$style_arr = array();

foreach($arr as $val)
{
$font_size = rand(12, 20).'px';
$font_color = 'rgb'.'('.rand(0, 255).','.rand(0, 255).','.rand(0, 255).')';
$style = '';
$font = '
';
$style_arr[] = $style.$val.$font;
}
shuffle($style_arr);
foreach($style_arr as $val2)
{
echo $val2.' ';
}
------解决方案--------------------

PHP code
$a = '长颈鹿,大象,小猫,老虎';$b = '兔子,猫肉,羊肉,牛肉';$c = '你好,谢谢,再见,很好';echo .c1 { background:red; font-size:12px; }.c2 { background:green; font-size:16px; }.c3 { background:orange; font-size:20px; }STYLE;$aa = explode(',', $a);array_walk($aa, 'func', 'c1');$bb = explode(',', $b);array_walk($bb, 'func', 'c2');$cc = explode(',', $c);array_walk($cc, 'func', 'c3');$ar = array_merge($aa, $bb, $cc);shuffle($ar);echo join('', $ar);function func(&$item, $key, $param) {  $item = "<span class="$param">$item</span>";}<div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template