Jump to content

Archived

This topic is now archived and is closed to further replies.

warnockm

Working With Array Keys

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,

Share this post


Link to post
Share on other sites
Try using a while loop,

while (count($_index_array) > 0)
{
// do stuff and here and remove the index from $_index_array upon success
}

Share this post


Link to post
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?

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

×

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.