T1000Android Posted November 26, 2011 Share Posted November 26, 2011 Hey all! I have the following code: <?php$playername0 = $_POST ["playername0"];$playername1 = $_POST ["playername1"];$playername2 = $_POST ["playername2"];$playername3 = $_POST ["playername3"];$playername4 = $_POST ["playername4"];$playername5 = $_POST ["playername5"];$playername6 = $_POST ["playername6"];$playername7 = $_POST ["playername7"];$playername8 = $_POST ["playername8"];$playername9 = $_POST ["playername9"];$player_name = array ($playername0, $playername1, $playername2, $playername3, $playername4, $playername5, $playername6,$playername7, $playername8, $playername9); sort ($player_name); echo $player_name;?> I have tryed just displaying the array unsorted and it doesnt work' I just get "Array" and thats it. With the sort function in there i get the same thing. Its like the array is empty. Please help! Link to comment Share on other sites More sharing options...
JamesB Posted November 26, 2011 Share Posted November 26, 2011 use print_r() to view array data, also you could simplify things with html attribute: name='playernames[]' and php: $playernames = $_POST['playernames']; which would create the array of names automatically. Link to comment Share on other sites More sharing options...
T1000Android Posted November 26, 2011 Author Share Posted November 26, 2011 The print_r code works but now i get: Array ( [0] => aab [1] => aac [2] => gbv [3] => gtrr [4] => gty [5] => hjk [6] => iih [7] => jyu [8] => opg [9] => zyu ) after inserting some values.Is there any way to print just the value in an ascending order? Link to comment Share on other sites More sharing options...
T1000Android Posted November 26, 2011 Author Share Posted November 26, 2011 I got it to work using a foreach loop: foreach ($player_name as $display){echo $display . "<br/>";} This code above only works for simple arrays. How do i get a multidimensional array to show me all its values?I want to see the values of an array within another array. Now i have this multidimensional array: $players = array ("playernames"=> array ($_POST["playername0"], $_POST["playername1"], $_POST["playername2"], $_POST["playername3"], $_POST["playername4"], $_POST["playername5"], $_POST["playername6"], $_POST["playername7"], $_POST["playername8"], $_POST["playername9"] ),"bestlaps"=> array ($_POST["bestlap0"], $_POST["bestlap1"], $_POST["bestlap2"], $_POST["bestlap3"], $_POST["bestlap4"], $_POST["bestlap5"], $_POST["bestlap6"], $_POST["bestlap7"], $_POST["bestlap8"], $_POST["bestlap9"] ),"totaltimes"=> array ($_POST["totaltime0"], $_POST["totaltime1"], $_POST["totaltime2"], $_POST["totaltime3"], $_POST["totaltime4"], $_POST["totaltime5"], $_POST["totaltime6"], $_POST["totaltime7"], $_POST["totaltime8"], $_POST["totaltime9"] ), "topspeeds"=> array ($_POST["topspeed0"], $_POST["topspeed1"], $_POST["topspeed2"], $_POST["topspeed3"], $_POST["topspeed4"], $_POST["topspeed5"], $_POST["topspeed6"], $_POST["topspeed7"], $_POST["topspeed8"], $_POST["topspeed9"] ),"avgspeeds"=> array ($_POST["avgspeed0"], $_POST["avgspeed1"], $_POST["avgspeed2"], $_POST["avgspeed3"], $_POST["avgspeed4"], $_POST["avgspeed5"], $_POST["avgspeed6"], $_POST["avgspeed7"], $_POST["avgspeed8"], $_POST["avgspeed8"] ));?> How do i sort each sub-array? Link to comment Share on other sites More sharing options...
JamesB Posted November 26, 2011 Share Posted November 26, 2011 try this: foreach ($players as $key => $playersArray) sort($players[$key]) Link to comment Share on other sites More sharing options...
T1000Android Posted November 26, 2011 Author Share Posted November 26, 2011 Thanks but i got is using this: sort ($players["playernames"], SORT_ASC);foreach ($players["playernames"] as $show) {echo $show . "<br/>";} but the problem is that i cant associate the playername with the bestlap, totaltime, topspeed and avgspeed.I want a table in which i can sort by all 5 parameters, but still keep the association between playername0, bestlap0, totaltime0, topspeed0 and avgspeed0. When i sort by 1 of the parameters, i still want to see the other 4 parameters by the parameter i just sorted by. Link to comment Share on other sites More sharing options...
T1000Android Posted November 27, 2011 Author Share Posted November 27, 2011 Anyone? Link to comment Share on other sites More sharing options...
Ingolme Posted November 27, 2011 Share Posted November 27, 2011 You should put all the information pertaining to each player into one associative array, then put each of those arrays into one larger array. After that, you can use usort() to sort the whole array by one type of data. In your form, organize the data like this: <input name="player[][name]" ... ><input name="player[][bestlap]" ...><input name="player[][totaltime]" ...> ... It will arrive with a structure like this: $_POST['player'] = array( 0 => array( name => "value", bestlap => "value", totaltime => "value" ), 1 => array( name => "value", bestlap => "value", totaltime => "value" ), 2 => array( name => "value", bestlap => "value", totaltime => "value" ), ...) Now, you can sort the $_POST['player'] array by one specific term using the usort() function: usort($_POST['player'], 'sort_by_name'); function sort_by_name( $a, $b ) { if($a['name'] > $b['name']) { return 1; } else { return -1; }} Link to comment Share on other sites More sharing options...
T1000Android Posted November 27, 2011 Author Share Posted November 27, 2011 Thanks! Never thought of using if else. Will this work for lets say, 16 players? Also what does 1 and -1 mean? Link to comment Share on other sites More sharing options...
Ingolme Posted November 27, 2011 Share Posted November 27, 2011 The parameters $a and $b represent two arbitrary elements of the array. When the sorting function returns a positive number, the first element goes after the second one, while a negative number causes it to go before the second one. A 0 means that the order doesn't matter and to leave them as they are. Link to comment Share on other sites More sharing options...
T1000Android Posted November 27, 2011 Author Share Posted November 27, 2011 Thanks! Youre the best. This is gonna require some extra thought from a noob like me. I will probably post some more questions later.Thanks again! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.