salibaray Posted January 18, 2010 Share Posted January 18, 2010 Hi there, I'am developing a snippet for a website widget that will read/fetch data from an XML file, sort it in array's and then show it back to the user - formated as a sentence. Example: XML sample: <row id="1"> <username>GGR1024</username> <name>Raymond</name> <surname>Saliba</surname> <game>Lotto</game> <won>5000</won> </row> <row id="2"> <username>GGR1111</username> <name>Isabelle</name> <surname>Bonnici</surname> <game>Lotto</game> <won>1000</won> </row> <row id="3"> <username>GGR1024</username> <name>Raymond</name> <surname>Saliba</surname> <game>Lotto</game> <won>2000</won> </row> <row id="4"> <username>GGR1024</username> <name>Raymond</name> <surname>Saliba</surname> <game>Lotto</game> <won>200</won> </row> So... I read that XML file line by line and put the values in an array, do some functions and then show the formatted data back to the user: Formatted data example: Raymond Saliba won $5000 from Lotto Isabelle Bonnici won $1000 from Lotto Raymond Saliba won $2000 from Lotto Raymond Saliba won $200 from Lotto (notice that 'Raymond Saliba' appears 3 times in this example) now the problem is this -> I need to only show 'Raymond Saliba' once to the user (each person is unique by his username) and the string that I show to the user must be the one that contains the highest ammount won. Proper output Example: Raymond Saliba won $5000 from Lotto Isabelle Bonnici won $1000 from Lotto So the blueprint behind my explenation is something like this: Person 1 ------> $5000 $2000 $200 Person 2 ------> $1000 (each person can have alot of values, and I need to show that user with the highest value - ONCE) Sounds easy right ? -> not for me... I'am not a professional developer with PHP but I tend to get along just fine, I have tried various methods for this snippet but so far I didn't find any luck I tried multidimensional arrays, array_unique, and some sorting functions but all ended with no luck at all... Can someone that understands what I need give me some advice/help please ? preferably with example code using the sample data that I gave to you in this post (because it makes me understand it better, sometimes I'm a slow learner sorry) Thank you very much and I'm awaiting your reply Raymond Saliba Link to comment https://forums.phpfreaks.com/topic/188869-need-to-get-a-unique-record-from-an-array/ Share on other sites More sharing options...
Catfish Posted January 18, 2010 Share Posted January 18, 2010 I think how difficult this can be to do could depend on how you store the data you retrieve from the XML. If I were to store data of this nature, I would structure an array like this: Array ( [$firstName.' '.$lastName] Array ( [$gameType] Array ( [i] = $winnings1 [i + 1] = $winnings2 etc ) ) ) Each person has an array of games, and each game has an array of winnings. Then to find the largest value of winnings from a game, you could loop through your array: $peoplesWinnings = readXML('blah'); // readXML() function will read data from XML into above format $personsName = $_GET['personsName']; // for example only. You could have an array full of names if you want $personsGame = $_GET['personsGame']; // as above // process winnings array to find highest value winnings only - if any foreach ($peoplesWinnings as $name => $gameArray) { foreach($gameArray as $gameName => $winningsArray) { $highestWinnings = 0; $highestIndex = 0; foreach($winningsArray as $winningsNum => $winningsAmount) { if ($winningsAmount > $highestWinnings) { $highestWinnings = $winningsAmount; // set the highest winnings amount unset($peoplesWinnings[$name][$gameName][$highestIndex]); // remove the previous winnings as they are less than the current $highestIndex = $winningsNum; // set highest winnings index name $peoplesWinnings[$name][$gameName]['highest'] = $winningsAmount; // maintain the position of the highest winnings in the array so we can find it easily later } else unset($peoplesWinnings[$name][$gameName][$winningsNum]); // remove the current winnings as they are less than the highest } unset($peoplesWinnings[$name][$gameName][$highestIndex]); // remove the final index (the highest winnings) as the highest winnings can now be found in index 'highest' } } This should hopefully work (code is untested) to return an array in the above format, with only the highest winnings for each game name per person. You could then output data from the array by knowing which person and game and using the ['highest'] index name like: print($personsName.' won $'.$peoplesWinnings[$personsName][$personsGame]['highest'].' from '.$personsGame.'.<br/>'."\n"); Hope this is what you were after. Link to comment https://forums.phpfreaks.com/topic/188869-need-to-get-a-unique-record-from-an-array/#findComment-997180 Share on other sites More sharing options...
salibaray Posted January 18, 2010 Author Share Posted January 18, 2010 This is very similar to what I want, I had tried to check for the highest value in the occuring loop and set a variable if that value is higher than the previous one (depending on the user), I will play with this code abit maybe I can get it to work (cause Im not that much of an array guy so this is quite new to me, but seems interesting and I will definatley check it out) Thank you for your help Catfish Link to comment https://forums.phpfreaks.com/topic/188869-need-to-get-a-unique-record-from-an-array/#findComment-997187 Share on other sites More sharing options...
salibaray Posted January 20, 2010 Author Share Posted January 20, 2010 Hi CatFish I have been playing around with the snippet that you gave me, its quite interesting but I still got stuck lol, This is the code you gave me: $peoplesWinnings = readXML('blah'); // readXML() function will read data from XML into above format $personsName = $_GET['personsName']; // for example only. You could have an array full of names if you want $personsGame = $_GET['personsGame']; // as above // process winnings array to find highest value winnings only - if any foreach ($peoplesWinnings as $name => $gameArray) { foreach($gameArray as $gameName => $winningsArray) { $highestWinnings = 0; $highestIndex = 0; foreach($winningsArray as $winningsNum => $winningsAmount) { if ($winningsAmount > $highestWinnings) { $highestWinnings = $winningsAmount; // set the highest winnings amount unset($peoplesWinnings[$name][$gameName][$highestIndex]); // remove the previous winnings as they are less than the current $highestIndex = $winningsNum; // set highest winnings index name $peoplesWinnings[$name][$gameName]['highest'] = $winningsAmount; // maintain the position of the highest winnings in the array so we can find it easily later } else unset($peoplesWinnings[$name][$gameName][$winningsNum]); // remove the current winnings as they are less than the highest } unset($peoplesWinnings[$name][$gameName][$highestIndex]); // remove the final index (the highest winnings) as the highest winnings can now be found in index 'highest' } } Variables $gameName, $gameArray and $winningsArray are not specified in the example that you gave to me, ie: you just use 3 variables which are -> $peoplesWinnings, $peoplesName and $peoplesGame. Can you pls give me a clearer example fo where each variable has to go in the code snippet that you provided ? it would help me understand it better (cause I got confused at first lol) Link to comment https://forums.phpfreaks.com/topic/188869-need-to-get-a-unique-record-from-an-array/#findComment-998541 Share on other sites More sharing options...
Catfish Posted January 20, 2010 Share Posted January 20, 2010 $gameName, $gameArray and $winningsArray are local variables which are created an only used in the scope of the foreach loops. Since they are created inside the foreach loops only, you do not need to specify or declare them. That is done when the foreach executes. $peoplesWinnings is an array with the format of the array i gave yu in my first post. $personsName and $personsGame are strings that in my example are shown to come from $_GET values suggesting you can pass those values from a form. For testing purposes, you can just set the strings directly like: $personsName = 'Joe Bloggs'; $personsGame = 'Lotto'; Link to comment https://forums.phpfreaks.com/topic/188869-need-to-get-a-unique-record-from-an-array/#findComment-998554 Share on other sites More sharing options...
ninedoors Posted January 20, 2010 Share Posted January 20, 2010 Maybe I am missing something but why not just do something like this: //Collect the data like this $person_wins[$row['username']] = array( 'name' => $row['name'].' '.$row['surname'], 'game' => array( 'game_name' => $row['game'], 'wins' => array(5000, 2000, 1000) ) ); //then loop thru the data foreach ($person_wins as $userid => $array) { //make new array with only one win per person $new_wins_array[$userid] = $array['name'].' won $'.max($array['game']['wins']).' playing '.$array['game']['name']; } The $row variable is just each xml element name. So $row['username'] is just the xml <username>. Then you just have to print the $new_wins_array or you can change the $new_wins_array[$userid] to an echo if you don't need it again. Nick Link to comment https://forums.phpfreaks.com/topic/188869-need-to-get-a-unique-record-from-an-array/#findComment-998751 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.