I have two questions.

1) How do you reindex an array?

Say my array has gaps in it like this:

array
[1] name1
[2]
[3] name2
[4]
[5]

I want to reindex it like this

[1] name1
[2] name2

2nd question is much more complicated. I am trying to match names.

I have two associate arrays which is a scoreboard. One of them is from a database.

Like this

name score
Fred 5
Barney 10

I am reading the other array from a database.

name score
Wilma 2
Barney 4

If the names match, I want to add their scores and put that in the database. If the names don't match then I want to just put their name and score in the database.

So Barney will have a 14, Fred will still have a 5 and is a new guy, and Wilma will remain unchanged.

I know how to get them to the database and from the database, what I am having trouble with is the actual array functions.

    Too tired to work out the second part of your problem, but as to the first part... look at [man]array_values()[/man]... the answer to your problems lies in the manual...

      Assuming that $DBArray has the names from the database and $NameArray has just the names you can try something like this:

      If there is only going to be one maximum occurance of name in either of the arrays (iow. Not wilma=>5 wilma=>2 wilma=>3 in the same array) you can maybe use something like this:

      // loop through every name in the DBArray
      foreach ($DBArray as $DBname => $DBscore) {
          // for every name in the nameArray, see if it matches the current name from the DBArray
          foreach ($nameArray as $name => $score) {
              if( $DBName == $name ) {
                  $newScore = $DBScore + $score;
              } else {
                  $newScore = $DBScore;
              }
              $newArray[$DBName] = $newScore;
          }
      }
      
      save_newArray_to_DB();
      

      Something like that anyways...

      Hope this helps.

      //sucks

        Write a Reply...