SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Loop through 3 element array to assign to 5 element array

    Confusing? yes. and I am not sure how to work it out.

    I have following scenario:
    - 3 vendors in array ([0]=>A,[1]=>B,[2]=>C)

    Now I need to assign this to another array which is 5 long for each day of the week (excluding Sunday and Saturday).

    So, lets say today is Wednesday then the output should be:
    [Wed] => A
    [Thu] => B
    [Fri] => C
    [Mon] => A
    [Tue] => B


    I can't figure out how to achieve this. I have no code to display as its just concept and am only thinking how to do it. Tried to write code and even tried to draw it out on paper but can't figure out an easier way to do it without having lots of if and for statements.

    Please help
    ---------------------------
    Errors = Improved Programming.
    My Site

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    If the first array is always the same, you can use the index of the array to determine the DAY.

    You can say M = 0 and F = 4.

    Then use the modulus operator to cycle the 5 into 3

    Example for Tues:
    1 % 3 = 1, so the value of Tuesday is $array[1], which is B.

    Example for Thursday:
    3 % 3 = 0, so the value of Thursday is $array[0], which is A.

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Is it true to say that from today (Friday) I want to use each supplier in turn (A, B, C) ad infinitum?

    Or could that change at some point, and that on another day I will switch and suddenly have only two, or have four, or five?

  4. #4
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First array is not same. It can change

    and yes, that array can change as we add/edit vendors. That's why the problem. Have to go to 5 days in the array and select 5 vendors (first one repeating if less than 5).

    Thanks
    ---------------------------
    Errors = Improved Programming.
    My Site

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    How many weeks worth of matches do you have to generate?

    How will you store the fact that the last used supplier was say, B, in order to generate the new set of matches when the first array of suppliers has to be regenerated? Is this in a text file, database?

  6. #6
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all the replies.

    It was to be for 5 days only. Now I say was, because the idea has been changed on how to take that approach so this is not needed anymore.
    ---------------------------
    Errors = Improved Programming.
    My Site


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •