duan19913 2012-05-09 16:12
浏览 60
已采纳

将颜色数组插入foreach循环中

$legend = array();
$color[] = ('000000', 'ff0000', 'fasd3f');
    foreach ($stats as $row) {

        if ($row->countofmsg > 0) {

            array_push($legend, "<div class='legend_label'><div class='color-block' style='background:#".$color.";'></div>".ucwords($row->msg)."<div class='legend_count'>$row->count</div></div><div class='clear'></div>");

        }

    }

Here is my code, what I would like to do is set an array of colors $color[], and then inside the foreach loop array, call the first color in the array and then call the second and third and so forth with each thing the foreach spits out. And then repeat at the beginning of the colors array when it reaches the last color in the array.

Would kick out something like:

(color1) msg - count
(color2) msg - count
(color3) msg - count
etc..

Please let me know if there is a duplicate question, I tried researching for it.

  • 写回答

3条回答 默认 最新

  • douyinmian8151 2012-05-09 16:19
    关注

    This assumes that your $stats array is integer indexed

    $legend = array();
    $color[] = ('000000', 'ff0000', 'fasd3f');
    $colorCount = count($color);
    foreach ($stats as $k => $row) {
        if ($row->countofmsg > 0) {
                $legend[] = "<div class='legend_label'><div class='color-block' style='background:#".$color[ ($k % $colorCount) ].";'></div>".ucwords($row->msg)."<div class='legend_count'>$row->count</div></div><div class='clear'></div>";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计