doufu9145 2013-05-29 11:54 采纳率: 0%
浏览 70
已采纳

在PHP(Cyclic)的foreach循环中返回数组的开头?

When you reach the end of an array in a PHP foreach loop, is there anyway to just start from the beginning again?

I'm building a graph using JS and PHP of a group of different people. I would like to have a different colour for each person on the graph but the number of people varies and is unknown.

I've built an array of hex codes to loop through but the problem is that when the number of people exceeds the number of hex codes in the array an error is thrown.

I realised I could just copy and paste the hex codes until there are more than the number of people but it doesn't seem like a very elegant solution.

Any thoughts?

  • 写回答

3条回答 默认 最新

  • doulu1907 2013-05-29 12:03
    关注

    Well, let's say N is length of your C array (colours) and P is your persons array.

    Leave foreach loop alone and use plain old for loop with i as loop variable. Then for person P[i] colour will be C[i%N]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?