PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hello everyone, hope all is well or getting better at least. Thank-you in advance for any help on this post. I have a question regarding reordering multidimensional arrays. A have a two-dimensional array for my baseball team that is populated from a MySQL database with, wins, losses,ties, etc,etc. After all of the scores are processed and the while loop is done I then calculate a winning percentage for each team based on their wins and losses. It adds it to the [8] column of the array. I then use
function reverse_compare($x,$y)
{
if ($x[8]==$y[8])
return 0;
elseif ($x[8]>$y[8]){
return -1;
else
return 1;
}
usort ($standings,'reverse_compare');
I have also created another multidimensional array to keep track of each teams head to head record (based on wins only). I created this array to help me deal with equal winning percentages, by this I mean, if two teams share an equal winning percentage I will evaluate their head to head records and if one of them has won more games against the other then I will place them higher in the standings. So my question is. How do you go about swapping just to rows of a multidimensional array only.
Cheers
Last edited by Todlerone on Wed May 07, 2008 10:34 am, edited 2 times in total.
Sorry. I simple print the array at the end after all sorting is done. I have no trouble reordering the entire array based on the winning percentage. I'm just not sure how to swap individual rows of an array. Would I just use the usort again and compare one element of one multidimensional array to one element of another multidimensional array? Or I'm I in left field on this one...pardon the pun.
Your call to usort() & it's subsequent calls to reverse_compare() do the swapping for you. Are you wanting to modify your reverse_compare() function so that in cases where it returns 0, it checks your second array to break the tie?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
pickle wrote:Your call to usort() & it's subsequent calls to reverse_compare() do the swapping for you. Are you wanting to modify your reverse_compare() function so that in cases where it returns 0, it checks your second array to break the tie?
pickle wrote:Did you read the examples on that page? They explain pretty well how to reference a global variable.
Sorry for the late reply pickle, the baseball season started so I had to make sure some form of a standings was displayed (even if it isn't sorted yet-oh and I have three kids). I have tried the global (as described on these pages). My problem with this now is how to add a comparison loop into this function. I'm only interested in the $x==$y part. So if there equal, go into another loop which compares a specific array that I have created to keep tract of each teams wins against each other. My problem is, in this function I'm not sure of the first array value it's at. $standings [0][8] through to $standings [7][8] are my teams. My best guess is that if I place my new comparison loop they will cycle equally?
pickle wrote:You have to declare $overall as global, at the beginning of the function.
I have done the global in the beginning part of this file, should I do it inside the function only? Sorry to not quite understand the placement of the global declaration. I have also placed the global $overall in the other file that populates the $overall array, is that needed there also.
As the documentation states, the global keyword tells PHP which scope you're referring to. Inside a function, you don't have access to variables outside the function's scope, unless they are globals or constants. You need to use the global keyword inside the function as well, so PHP knows which scope to look in.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.