Jump to content


Photo

Working With Array Keys


  • Please log in to reply
5 replies to this topic

#1 warnockm

warnockm
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 29 June 2006 - 03:51 PM

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,

#2 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 29 June 2006 - 03:53 PM

Try using a while loop,

while (count($_index_array) > 0)
{
// do stuff and here and remove the index from $_index_array upon success
}
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#3 warnockm

warnockm
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 29 June 2006 - 04:19 PM

[!--quoteo(post=389312:date=Jun 29 2006, 11:53 AM:name=Buyocat)--][div class=\'quotetop\']QUOTE(Buyocat @ Jun 29 2006, 11:53 AM) View Post[/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?

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 29 June 2006 - 04:25 PM

You can get th for loop to work by adding -1 after the count function, like so:
for ( $a = 0; $a < count( $array)-1; $a++ ) {
//
}
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.

#5 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 29 June 2006 - 04:26 PM

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.
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#6 warnockm

warnockm
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 29 June 2006 - 04:49 PM

[!--quoteo(post=389324:date=Jun 29 2006, 12:26 PM:name=Buyocat)--][div class=\'quotetop\']QUOTE(Buyocat @ Jun 29 2006, 12:26 PM) View Post[/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?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users