Diegar Posted October 7, 2007 Share Posted October 7, 2007 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 More sharing options...
Diegar Posted October 7, 2007 Author Share Posted October 7, 2007 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 More sharing options...
Synook Posted October 8, 2007 Share Posted October 8, 2007 Uh... in_array() Link to comment Share on other sites More sharing options...
Diegar Posted October 8, 2007 Author Share Posted October 8, 2007 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 More sharing options...
justsomeguy Posted October 8, 2007 Share Posted October 8, 2007 What does the structure of the final array that you want to check look like? Do a print_r on it and post the structure here. Link to comment Share on other sites More sharing options...
Diegar Posted October 8, 2007 Author Share Posted October 8, 2007 Array( [0] => Array ( [0] => Steve [1] => 10 ) [1] => Array ( [0] => Robert [1] => 9 ) [2] => Array ( [0] => Tony [1] => 8 )It goes on, but you can see the structure from this i think. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 8, 2007 Share Posted October 8, 2007 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 More sharing options...
Diegar Posted October 8, 2007 Author Share Posted October 8, 2007 I haven't tried what you said yet, but i never thought about doing it that way. You are brilliant. Thank you! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.