Jump to content

last(), current(), and next() with a multidimensional array


Errant_Shadow

Recommended Posts

I'm using the array's internal pointer to navigate through my arrays to analyze the data

 

what I've been doing is setting up a for loop, but using the current() and next() in lieu of the traditional variable ($i). And I know, foreach does exactly that, but I've been having a lot of trouble getting foreach to do what I want. It keeps only running it once as far as I can tell.

 

Anyway, basically I have an array of arrays, where each sub array contains various character data. What I've been doing is using $r as my navigation variable in a for loop, ending up with blocks of code like $raceArray[$r]['name']. Now, this works, except for some reason it never reads the last element. I think this has something to do with the fact that my array indexes range from 1-10 rather than 0-9; although I've tried adding 1 to sizeof($raceArray) but it doesn't seem to help.

 

Which brings me to using current() and next() but for the life of me I can't figure out how to use these to navigate a multidimensional array... the one that's really getting me is using this technique inside an additional associative sub array (where I can't even get a traditional for loop to work at all)

 

what I'm doing now is something like current($raceArray[$r]['damage']) to check the data held in each element of the damage array, which is contained inside the character array inside the race array... any advice?

Link to comment
Share on other sites

How do you want to analyze the data? I would just have a function like -

 

// the $increment is so we don't blow up Apache running
// an array of 100 sub arrays.
function traversal ($array, $increment = 0) {
     if ($increment === 10) return false;
     for ($e = current($array); next($array); ) {
          if (is_array($e)) return traversal ($e, $increment++);
          if ($e == "some comparison stuff") return true;
     }
     return false;
}

 

Again, it's bland. You need to be more specific and possibly post up some code.

Link to comment
Share on other sites

I know. My brain is kind of fried trying to track all of the data I'm using, but I'll try to be as simple as I can to convey my problem.

 

So what I have is an array of arrays. It is called racerArray and it is composed of all the character data for each racer participating in the race that my script runs. This is *technically* ordered by index, but the index starts at 1 rather than 0, so its easier to think about it as an associative array where each key represents a racer's position in the race, from first to last.

 

The individual characer arrays are assembled from data I fetch from MySQL and contain the character's name, account name, current experience points, etc etc, but more importantly, it contains their skill ranks, their bike's stats, and the damage value for each part (which is in it's own sub array inside the character array).

 

Additionally, I have a multidimensional associative array much in the same fashion for holding the track. It is called the trackArray and each element (starting at 1) holds the data for that individual segment, which includes the segment's length, the condition, a hazard for the racers to overcome, and an array of stat modifiers to apply to each racer's bike stats.

 

So basically, what I need to do is walk through the trackArray, and at each segment, use the difficulty mod, the length, the condition and hazard information, and the array of stat modifiers to calculate how each racer did; if they crashed, how much damage each part took on this segment, of a part broke down, and then (if they made it through the segment) how well they did in a way I can compare to each other racer.

 

Now, I have a model that does all that and worked well enough for my purposes, but i've had to strong-arm, and make work-arounds for a lot of it and I was just hoping that there might be an easier way to accomplish my end goal.

 

Edit: I will post some code, but I'm swamped at the moment and I have to clean up all my neurotic notes and such first.

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.