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条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统