cliftonbazaar Posted March 16, 2018 Share Posted March 16, 2018 I have an array of $my_heroes = ("Batman", "Superman", "Spiderman", "Hulk"); Note that this array will expand over time as more 'Heroes' are added. Each Hero is to have a value added, depending on several things, and then sorted at the end. My thinking is to have a 2 Dimensional array. So my question is how do I change my array into a 2D array and then add a value, so for example after round one of fighting I wish to give Batman 10 points, Superman 15 points and Hulk 20 points. I have tried $$my_heroes[0] += 10; but this turned out to be a disaster. So the end result I want is $my_heroes_results[0][0]="Batman"; $my_heroes_results[0][1]=10; $my_heroes_results[1][0]="Superman"; $my_heroes_results[1][1]=15; $my_heroes_results[2][0]="Spiderman"; $my_heroes_results[2][1]=0; $my_heroes_results[3][0]="Hulk"; $my_heroes_results[3][1]=20; Then I can sort them into order. Quote Link to comment Share on other sites More sharing options...
Solution benanamen Posted March 16, 2018 Solution Share Posted March 16, 2018 (edited) You mean like this? $my_heros = [ 'Batman' => 0 , 'Superman' => 0 , 'Spiderman' => 0 , 'Hulk' => 0 ]; $my_heros['Batman'] = 10; $my_heros['Superman'] = 15; $my_heros['Hulk'] = 20; asort($my_heros);// Sorted by Points echo '<pre>'; print_r($my_heros); echo '</pre>'; Result: Array([spiderman] => 0[batman] => 10[superman] => 15[Hulk] => 20) Edited March 16, 2018 by benanamen 1 Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted March 16, 2018 Share Posted March 16, 2018 a) you should be using a database for this, it will be much easier to handle new data and get the results you want, and b) even if not using a database, your data should be normalized and have an audit trail, where each different type of data is stored in its own array/table and each transaction/round that affects a value is a separate entry in the array/table, not just a number that gets updated. you would have a hero array/table that defines the hero names and assigns them unique ids. the points should be a separate array/table with a hero id, a round number, and point value. to get the total points for any/all heroes, you would sum up the point values for each hero id. if you want the results sorted by the total points, you apply the array sort function to the result array holding the sums. all pretty much the same way that a database engine would do this based on an sql query. Quote Link to comment Share on other sites More sharing options...
cliftonbazaar Posted March 16, 2018 Author Share Posted March 16, 2018 Thanks Benanamen, that's excactly what I'm after. This code is working out ratings based on what is in the database, so all the data has been stored, I now have to work out the results. This is part of a much bigger project so I need to be able to integrate the data with other parts which this array has done. Quote Link to comment Share on other sites More sharing options...
benanamen Posted March 16, 2018 Share Posted March 16, 2018 Since you have the data in a DB, do the work in the query. There is no need for array gymnastics. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.