Jump to content

Comparing Arrays and adding tally


Diegar

Recommended Posts

Hello. I have a loop that grabs the first ten names in a table, and their ID's, which show their order. The line number in the loop adds a score to the name, based on it's position in the table. I leave the ID out of the array, but put the name and score:Array ( [0] => Robert [1] => 10 ) Array ( [0] => Steve [1] => 9 ) Array ( [0] => Harry [1] => 8 ) etc.. until '[2] => 1'The code will then go to the next usertable and do the same thing for the names in that list.The idea is to have all of these values inserted into a single array, but if the name already exists, the values are added together, instead of duplicating the name:The final array already has in it:Array ( [0] => Robert [1] => 10 ) and the next list has:Array ( [0] => Robert [1] => 8 ) In the final array, i was hoping to end up with the result of:Array ( [0] => Robert [1] => 18 ) Hope that was clear enough.... If not, feel free to ask for more. Anyone have any suggestions?

Link to comment
Share on other sites

Okay.. maybe i can make this is a little easier, as far as what to ask.If i have a string with a value in it of "Robert". Is there a way for me to check an array, for any values equaling "Robert"?

Link to comment
Share on other sites

Uh... in_array()
I tried that, as well as array_search. Neither worked, cause the example of Robert is an array, not a string. I can pull it out to a string, but i can't seem to compare it to a string with either of those, unless i tell it exactly where "Robert" is in the array, which defeats the purpose.
Link to comment
Share on other sites

If you need a multidimensional array like that then you will need to loop through the structure yourself and look to see if a certain name is there. Or, if all you're keeping track is a number associated with each name, it would be better to use the name as the array key and the number as the value. Then you can use isset to check if a user is already there.$users = array("Steve" => 10,"Robert" => 9,"Tony" =>8);

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...