warnockm Posted June 29, 2006 Share Posted June 29, 2006 Hi, i have a problem i can't seem to figure out. I'm using two arrays to create columns for a report. Basically one array has all of the columns you can use and the other has which ones are actually in the report. Once you add one, to the report, it should disappear from the first array.I'm not sure how to get the index's of the values in teh array. for instance. if i have elements 0 through 5 (total of 6) and I remove #3 (now a total of 5), how do i loop through the remaining elements and still use the indexs?for ( $a = 0; $a < count( $array); $a++ ) { // } doesn't work, because when i remove elements, the count changes. the count becomes 5 and it goes from 0 to 4 instead of 5.foreach( $array as $val ) { // } doesn't work because now i don't know the indexes. so is there a way to get the index number of a value, or loop through array w/o using count, but still see the indexes? i don't mind checking if the array element exists.thanks, Quote Link to comment https://forums.phpfreaks.com/topic/13218-working-with-array-keys/ Share on other sites More sharing options...
Buyocat Posted June 29, 2006 Share Posted June 29, 2006 Try using a while loop,while (count($_index_array) > 0){// do stuff and here and remove the index from $_index_array upon success} Quote Link to comment https://forums.phpfreaks.com/topic/13218-working-with-array-keys/#findComment-50878 Share on other sites More sharing options...
warnockm Posted June 29, 2006 Author Share Posted June 29, 2006 [!--quoteo(post=389312:date=Jun 29 2006, 11:53 AM:name=Buyocat)--][div class=\'quotetop\']QUOTE(Buyocat @ Jun 29 2006, 11:53 AM) [snapback]389312[/snapback][/div][div class=\'quotemain\'][!--quotec--]Try using a while loop,while (count($_index_array) > 0){// do stuff and here and remove the index from $_index_array upon success}[/quote]Won't this do the same thing as a foreach loop? How do i get the index value of the array? Quote Link to comment https://forums.phpfreaks.com/topic/13218-working-with-array-keys/#findComment-50887 Share on other sites More sharing options...
wildteen88 Posted June 29, 2006 Share Posted June 29, 2006 You can get th for loop to work by adding -1 after the count function, like so:[code]for ( $a = 0; $a < count( $array)-1; $a++ ) {//} [/code]The key here is the use of -1, as the count will say there are five values in the array. Burt as arrays start from zero we need to subtract one form what count returns. Now the for loop will work. Quote Link to comment https://forums.phpfreaks.com/topic/13218-working-with-array-keys/#findComment-50889 Share on other sites More sharing options...
Buyocat Posted June 29, 2006 Share Posted June 29, 2006 It looks like your for loop would work just fine...try thisfor ($i = 0; $i < count($array); $i++){// do some stuff here// if you remove a value from $array then do this$i = 0;}So, you start the loop over every time you remove a value, that way you'll be sure not to skip anything. The while loop I had suggested before just checks for the condition, you're right it may not help if you need to be at the ith place in both arrays at once, but if you can coordinate the arrays without using $i then it may still work. Quote Link to comment https://forums.phpfreaks.com/topic/13218-working-with-array-keys/#findComment-50890 Share on other sites More sharing options...
warnockm Posted June 29, 2006 Author Share Posted June 29, 2006 [!--quoteo(post=389324:date=Jun 29 2006, 12:26 PM:name=Buyocat)--][div class=\'quotetop\']QUOTE(Buyocat @ Jun 29 2006, 12:26 PM) [snapback]389324[/snapback][/div][div class=\'quotemain\'][!--quotec--]It looks like your for loop would work just fine...try thisfor ($i = 0; $i < count($array); $i++){// do some stuff here// if you remove a value from $array then do this$i = 0;}So, you start the loop over every time you remove a value, that way you'll be sure not to skip anything. The while loop I had suggested before just checks for the condition, you're right it may not help if you need to be at the ith place in both arrays at once, but if you can coordinate the arrays without using $i then it may still work.[/quote]I don't think this work work. picture the scenario where i have 6 values (index 0 -> 5). I then remove #2 and #4. I'm left with elements 0, 1, 3, 5. I now only have 4 elements in the array. If i loop i = 0 to 4, i is going to be 0, 1, 2, 3 but i need it to be 0, 1, 3, 5. Or i need it to be 0, 1, 2, 3, 4, 5 and check of the element exists. it would not on #2, and #4. Am i totally missing something?thanks everyone,edit: i'm using unset() to remove the value. is that correct? Quote Link to comment https://forums.phpfreaks.com/topic/13218-working-with-array-keys/#findComment-50902 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.