1

I'm little confused as in how can I use do-while loop in an associative array in php. I don't have ordered indexes starting from 0 so I can't iterate simple as in case of C or Java

NOTE :: I specifically want do-while sort of behavior...i.e. loop s'd execute atleast once in the beginning

user401445
  • 1,014
  • 1
  • 16
  • 41
  • 4
    What do you want to accomplish? Perhaps you should just be using `foreach ($array as $key=>$value) {}` – Michael Berkowski Nov 08 '11 at 13:53
  • In PHP, `foreach` is typically used far more often than an indexed for loop (as in `for ($i=0; $i<10; $i++)`) – Michael Berkowski Nov 08 '11 at 13:54
  • This should answer your question: http://stackoverflow.com/questions/1951690/php-how-to-loop-through-a-associative-array-and-get-the-key-name – Polynomial Nov 08 '11 at 13:54
  • No, I specifically want do-while sort of behavior...i.e. loop s'd execute atleast once – user401445 Nov 08 '11 at 13:58
  • @user401445 Then it depends entirely on the kind of condition you want to evaluate. We need an example of what you want to achieve. – Michael Berkowski Nov 08 '11 at 13:59
  • I can accomplish what I want using a separate function but seriously is there no iterator kind of behavior by which I can accomplish the same using any loop ? – user401445 Nov 08 '11 at 14:02
  • 1
    @user401445 You're not getting me. You can't ask generically if a do-while can be used with an indexed array. Of course it can, it just depends on what you need to do. We need to see what problem, specifically, in code, you are trying to solve. You can evaluate conditions against the return of `array_keys()` for example, or many other possiblities. – Michael Berkowski Nov 08 '11 at 14:05
  • 1
    -1 for not posting the actual code you have trouble with. – Your Common Sense Nov 08 '11 at 14:09
  • @Col.Shrapnel actual code is too complex to post – user401445 Nov 08 '11 at 14:16
  • @Michael Yes..array_keys() is what I was looking for :) – user401445 Nov 08 '11 at 14:19
  • @user401445: I still dont get why you need such an "emulation" of a do-while if you could use `foreach`? – scube Nov 08 '11 at 14:23
  • @scube I was having an array of results which I had to group in my loop based on some conditions...and was printing that when first member of next group of result was found. That printing condition was not getting executed for the last group of results... so do-while kinda behavior was something I wanted... didn't wanted to write any hacky code – user401445 Nov 08 '11 at 14:33
  • you can always create a simplified test case. **especially** if you are investigating whatever new feature. make it work in a distinct simplified code, and then merge it into your main project. – Your Common Sense Nov 08 '11 at 14:39
  • without providing a real case nor real data you are limiting yourself to just one solution, which was posted by someone out of *guess*. if I were you, I wouldn't waste my time *such unproductive way* – Your Common Sense Nov 08 '11 at 14:41
  • @Col.Shrapnel Will keep that in mind next time :) – user401445 Nov 08 '11 at 15:23

2 Answers2

4

You can use the array_keys function.

$keys = array_keys($assocArray);
if(!empty($keys)) do{
  $key = array_pop($keys);
  // ...
}while(!empty($keys));

If you really want to use a do-while loop. But if a simple while loop is good for you, the first if isn't neccessary:

$keys = array_keys($assocArray);
while(!empty($keys)){
  $key = array_pop($keys);
  // ...
};

edit:

If you really want your loop to run at least once:

$keys = array_keys($assocArray);
do{
  $key = array_pop($keys);
  if($key===NULL){
    // first (and last) run for an empty array
  }else{
    // ...
  }
}while(!empty($keys));
Dutow
  • 5,638
  • 1
  • 30
  • 40
2

If you just need to iterate the full array you should use foreach:

foreach($array as $index => $value)
{
  // do some stuff
}

Of if you dont care about the index of your array:

foreach($array as $value)
{
  // do some stuff
}
hakre
  • 193,403
  • 52
  • 435
  • 836
scube
  • 1,931
  • 15
  • 21