Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP help: while loop using list() and each()

Options
  • 11-08-2009 6:25pm
    #1
    Closed Accounts Posts: 12,382 ✭✭✭✭


    Hello

    I hope someone can help me with this.

    I am using a while loop as follows:

    [PHP]while(list($catNumber,$catText) = each($categories)) {
    ...
    }[/PHP]

    And the $categories it refers to are as follows:

    [PHP]
    $categories = array
    ("09"=>"1001",
    "29"=>"1002",
    "18"=>"1003",
    "18"=>"1004",
    "17"=>"2000",
    "16"=>"2001",
    "17"=>"2005",
    "17"=>"2006",
    "09"=>"2090",
    "09"=>"3001",
    "09"=>"3002",
    "09"=>"3004",
    "22"=>"3006",
    "19"=>"3090",
    "19"=>"4001",
    "09,12"=>"4003",
    "09"=>"4025",
    "09"=>"4026",
    "09"=>"4027",
    "09"=>"4040",
    "23"=>"105000",
    "04,12"=>"5090",
    "12"=>"6001",
    "12"=>"6002",
    "12"=>"60021",
    "03,12"=>"6003",
    "12,24"=>"6005",
    "04,15"=>"7001",
    "15"=>"7002",
    "15"=>"7003",
    "04"=>"7090",
    "03"=>"8000",
    "03"=>"8001",
    "04"=>"10000",
    "25"=>"10003",
    "25"=>"10004",
    "25"=>"10005",
    "20"=>"11001",
    "20"=>"11005",
    "21"=>"11006",
    "04,29"=>"13090",
    "29"=>"14001",
    "29"=>"14002",
    "29"=>"14004",
    "04,23"=>"11090",
    "23"=>"12001",
    "23"=>"12003",
    "07,08"=>"13001",
    "08,19"=>"13030",
    "08,19"=>"13050",
    "08"=>"13070",
    "08"=>"13080",
    "14"=>"15002",
    "14"=>"15004");
    [/PHP]

    The problem is as follows:

    The each() function is not sequentially moving through the $categories array - it jumps around in what appears to be a fairly random order.

    Therefore it is reaching the last element '"14"=>"15004"' before it reaches some of the other elements. This means the loop is ending before all the elements have been processed.

    Is there any way I can force each() to go through the array in some sort of sequential order, or at least force each() to go through every element in the array?

    Thanks!


Comments

  • Closed Accounts Posts: 7,794 ✭✭✭JC 2K3


    Are you calling 'reset ($categories);' before the while loop?


  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    Thanks for the reply.

    No, I am not doing that. I thought the pointer should be at the beginning by default?


  • Closed Accounts Posts: 7,794 ✭✭✭JC 2K3


    Well, I don't know what your code is doing before that. I'm not familiar with the syntax, but the PHP Manual for each() and the comments beneath seem to recommend always calling reset() before doing this.

    Just looking at it again, can you have duplicate keys like that in an array? I really don't think you can....


  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    Doh! I think you might be onto something there. I'll try swapping the values (which are all unique) with the keys (which are not unique.)

    Thanks for the help.


  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    In fact, you're definitely right. I'm not sure what I was thinking.

    Cheers!


  • Advertisement
  • Closed Accounts Posts: 7,794 ✭✭✭JC 2K3


    No probs.


Advertisement