Jump to content

Working With Array Keys


warnockm

Recommended Posts

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,
Link to comment
Share on other sites

[!--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?
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

It looks like your for loop would work just fine...

try this
for ($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.
Link to comment
Share on other sites

[!--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 this
for ($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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.